0

I have foreach row in my datagrid an checkbox:

  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Number}" Header="Name"/>
    <DataGridTemplateColumn Header="Watched">
      <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
          <CheckBox x:Name="checkWatched" IsChecked="{Binding Path=Watched, Mode=TwoWay}" Click="checkWatched_Click" />
        </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
  </DataGrid.Columns>

My database contains the value for the checkbox (true or false). How can I foreach row in the datagrid set the value of the checkbox?

1 Answers1

0

Create a class (we'll call it MyObject for now) that implements INotifyPropertyChanged. Create a property in your MyObject called "Watched" of type bool. In the setter, raise your property changed event. This is necessary if you want the grid to reflect any changes to the data when anyone calls the setter.

Create a collection of MyObject called WatchedList or whatever. Now "foreach" item in your database, add a new object to your WatchedList reflecting the value in your database.

Then in your XAML, on the DataGrid you'll set ItemsSource = "{Binding WatchedStatuses}".

Then you can get the the value of Watched from each item in WatchedList from your code behind.

NielW
  • 3,626
  • 1
  • 30
  • 38