Murilo lima alves
Murilo lima alves

Reputation: 55

WPF DataGridTemplateColumn - Trigger for CheckBox IsChecked Property

I am creating a WPF DataGrid control with a DataGridTemplateColumn CheckBox which should have the IsChecked property set as true or false depending on a DataTable value)...

If the DataTable row has the value set as 1, the DataGridTemplateColumn should be true (checked). Otherwise, it will be false (unchecked)... by default, every new row will be set as true, but I couldn't bind these CheckBox rows to my DataTable value (which will be 1 or 2)

I tried the following c# code. It works when I finish the UserControl edition and loop through my DataTable to save its values... But, when I set gridResultados.ItemsSource = dataTable.DefaultView clearly not checking/unchecking my CheckBoxes because there is no binding between the DataTable column value (1 or 2) and my DataGridTemplateColumn ... this is my closest approach:

<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Value?" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="True" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">
                <Style TargetType="{x:Type CheckBox}">
                    <Style.Triggers>
                        <Trigger Property="Content" Value="1">
                            <Setter Property="IsChecked" Value="True"/>
                        </Trigger>
                        <Trigger Property="Content" Value="2">
                            <Setter Property="IsChecked" Value="False"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </CheckBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    dataTable.Rows[gridResultados.SelectedIndex].SetField(4, 1);
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    dataTable.Rows[gridResultados.SelectedIndex].SetField(4, 2);
}

Upvotes: 1

Views: 4632

Answers (1)

King King
King King

Reputation: 63317

Firstly I recommend you not to use DataTable in WPF, because we usually use EF in WPF, the DataTable is part of ADO.NET and it's old and nearly deprecated.

For your problems, you have somethings to fix involving the Binding and the Trigger. First the implicit DataContext in each row here is a DataRowView. It's available only for DataTrigger to use (not for Trigger). So you need to use DataTrigger instead to listen to the index [4] (which references to the fifth column - the column of CheckBox). Second the Style you set is wrong, it should be wrapped in CheckBox.Style to make it understand that you set Style for CheckBox, not for its Content. Third the Style Trigger cannot override locally set IsChecked (as in your code), in this case the template Trigger can override that, but you can also use a Style Setter to set default value True for IsChecked. Here is the code you should have:

<DataGridTemplateColumn Header="Value?">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">
            <CheckBox.Style>
               <Style TargetType="{x:Type CheckBox}">
                  <Setter Property="IsChecked" Value="True"/>
                  <Style.Triggers>
                    <DataTrigger Binding="{Binding [4]}" Value="1">
                        <Setter Property="IsChecked" Value="True"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding [4]}" Value="2">
                        <Setter Property="IsChecked" Value="False"/>
                    </DataTrigger>
                  </Style.Triggers>
              </Style>
            <CheckBox.Style>
        </CheckBox>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

BTW, I can see that you use event handler to update the value of the underlying data. But that's not the right way to go in term of MVVM where almost what you need is Binding and related stuff. However that part is yours, you need to research more on that. I think it's enough to end this problem here.

Upvotes: 1

Related Questions