2012-03-08 4 views
1

私は、次のXAML持っている:私はNodeListのように持っているServiceMapViewModelでリスト<string>をDataGridColumnにバインドするにはどうすればいいですか?

private ServiceMapViewModel smViewModel = new ServiceMapViewModel(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     this.Loaded += (s, e) => 
      { 
       this.DataContext = smViewModel; 

      }; 
    } 

<DataGrid Name="nodeDataGrid" ItemsSource="{Binding NodeList}" AutoGenerateColumns="False" RowBackground="White" 
      AlternatingRowBackground="AliceBlue" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
      Background="Silver" Margin="0,34,10,10" IsReadOnly="True" > 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" CanUserSort="True" SortDirection="Ascending" CellStyle="{StaticResource CellStyle}" /> 
      <DataGridTextColumn Header="Category" Binding="{Binding Path=Category}" Width="*" /> 
      <DataGridTemplateColumn Header="Children"> 
       <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <ListBox> 
          <TextBlock Text="{Binding}" /> 
         </ListBox> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
      </DataGridTemplateColumn> 
     </DataGrid.Columns> 
    </DataGrid> 

そして、背後にあるコードをリスト<ノード>

、ノードは次のとおりです。

public string Name { get; set; } 

    public string Category { get; set; } 

    public string Group { get; set; } 

    public List<string> Children { get; set; } 

    public List<string> Parents { get; set; } 

私の質問は、どのようにlistb牛とChidrenの財産?

+0

あなたはItemSource = {バインディング子供} – Lloyd

+0

はのItemsSourceがすでに持っているので、はい、私は試みたが、例外をthrowns試してみましたデータグリッドで初期化されました – Bip

答えて

3

Childrenは、現在のコンテキストのコレクションです:

<ListBox ItemsSource="{Binding Children}"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <TextBlock Text="{Binding}" /> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

TNXの作業として表示されています。私のタイムゾーンでは遅すぎて、私の脳が機能しなくなります。テンプレートなしでリストボックスにテキストブロックを書きました。 – Bip

0

ただし、NodeクラスにINotifyPropertyChangedを実装しない限り、バインディングに反映されたプロパティは更新されません。

編集:まあ、あなたは配列内の配列をバインドしています。次に、TextBlockではなくコレクションに実際にバインドできるコントロールが必要になります。または、あなたはただ1つのノードをバインドしようとしていますか?

多分あなたがしようとしていることを少し良く説明することができます。次を使用する必要がありますので、

+0

残念ながら、それは動作しません。はい、私はViewModelでINotifyPropertyChanedを実装しており、NameとCategory DatagridColumnに適切な値を持っています! EDIT:(コレクション) – Bip

関連する問題