MKII
MKII

Reputation: 911

Data Binding to the contents of a list

I am trying to bind the properties of the items in a list to a data grid. I have the following code:

  <DataGrid Grid.Column="0" Name="LeftSettingsDataGrid" ItemsSource="{Binding Path=Data}">
    <DataGrid.Columns>
      <DataGridTextColumn Header="Setting Name" Binding="{Binding Path=Settings.SettingName}" />
      <DataGridTextColumn Header="Setting Type" Binding="{Binding Path=Settings.SettingType}" />
    </DataGrid.Columns>
  </DataGrid>

But this produces an empty grid view.

Data is an object that has a list of Settings, which are objects that have two public properties: SettingName and SettingType.

Basically, the hierarchy is like this: 1. Data 1. Settings 1. SettingName 2. SettingType

The data context is set in the main window's constructor, with LeftSettingsDataGrid.DataContext = Data;.

Why is the binding not working then?

Upvotes: 0

Views: 106

Answers (1)

Boo
Boo

Reputation: 673

See this as a tree in your datacontext you have a list that you display as many items, your item source is Settings, your display member is item.SettingName etc

  <DataGrid Grid.Column="0" Name="LeftSettingsDataGrid" ItemsSource="{Binding Path=Settings}">
    <DataGrid.Columns>
      <DataGridTextColumn Header="Setting Name" Binding="{Binding Path=SettingName}" />
      <DataGridTextColumn Header="Setting Type" Binding="{Binding Path=SettingType}" />
    </DataGrid.Columns>
  </DataGrid>

your ItemsSource is Data.Settings not Data

Upvotes: 1

Related Questions