Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom CheckBox in WPF DataGrid does not update binding

I have the following (simplified) style:

<Style x:Key="MyStyle" TargetType="{x:Type CheckBox}">  
    <Setter Property="Background" Value="Blue" />  
</Style>

If I use it as the ElementStyle AND EditingElementStyle in my DataGridCheckBoxColumn:

<DataGridCheckBoxColumn Binding="{Binding IsEnabled}"  
                        ElementStyle="{StaticResource MyStyle}"  
                        EditingElementStyle="{StaticResource MyStyle}" />

Then my binding, IsEnabled, does not toggle when I check/uncheck a row's checkbox. If I remove either ElementStyle, EditingElementStyle, or both, then the binding updates no problem. Why is this?!

Also, I tried to work around the problem using the following code:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsEnabled}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

However, the problem remains.

like image 800
Pakman Avatar asked May 28 '10 03:05

Pakman


People also ask

How to add checkbox in DataGrid?

You can use template column to add checkbox in datagrid . How to read the check box status in c# for above code. Please Sign up or sign in to vote. I went with very simple..

How do I bind a three-state checkbox to a view model?

Now you can bind the three-state CheckBox control’s IsChecked property to the Countries property of the view model using a converter. Converters change data from one type to another and provide a way to apply custom logic to a data binding.

Why is my data binding not working in Visual Studio?

Binding is not working and ui is not updating.i am following MVVM pattern. Check the output window in Visual Studio for data binding errors. Please Sign up or sign in to vote. Thus, your property is also not making use of interface making sure the changes can reflect.


1 Answers

Sorry for the necro, but I think I found a better solution here on Stack Overflow that might help people ending up on this page searching for a solution.

https://stackoverflow.com/a/7270548/3082531

<DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
        <CheckBox IsChecked="{Binding Path=IsSelected, UpdateSourceTrigger=PropertyChanged}" />
    </DataTemplate>
</DataGridTemplateColumn.CellTemplate>

I tried this and it worked perfectly for me, simpler than the accepted solution and also eliminating the need for extra clicks on the checkboxes.

like image 96
supersonic_ht Avatar answered Oct 10 '22 14:10

supersonic_ht