Reputation: 95
I am using a WPF DataGrid in a simple Window to display some Data provided by a Model.
<DataGrid x:Name="unitTable" ItemsSource="{Binding Units}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsChosen, Mode=TwoWay}" Header="Chosen"></DataGridCheckBoxColumn>
<DataGridTextColumn Binding="{Binding Path=Name, Mode=TwoWay}" Header="Name"></DataGridTextColumn>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=IsActive, Mode=TwoWay, Converter={StaticResource boolToActive}}" Header="Active"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Everything worked perfectly fine with the model, but there was this problem where I had to double click the columns to edit the value of the checkboxes for example.
I came up with this solution:
<Style TargetType="DataGridCell" x:Key="NoDoubleClick">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEditing" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
And after I assigned the style to the Checkbox- and the first TextColumn it actually worked :) After a while I realized, that the model doesn't get updated anymore when the Style "NoDoubleClick" was assigned to a column.
Can anybody tell me what I am doing wrong?
Upvotes: 3
Views: 543
Reputation: 145
Manfred Radlwimmer is right.
UpdateSourceTrigger=PropertyChanged
is needed to update the model.
<DataGrid x:Name="unitTable" ItemsSource="{Binding Units}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding Path=IsChosen, Mode=TwoWay}" Header="Chosen"></DataGridCheckBoxColumn>
<DataGridTextColumn Binding="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header="Name"></DataGridTextColumn>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding Path=IsActive, Mode=TwoWay, Converter={StaticResource boolToActive}}" Header="Active"></DataGridTextColumn>
</DataGrid.Columns>
Upvotes: 0
Reputation: 13394
DataGridColumns usually only update the bound data when the edited cells lose focus. To get around this and make it work with your Style, set the UpdateSourceTrigger
of your Binding to PropertyChanged
.
Upvotes: 2