Wpf MVVM and editing DatagridCell


Wpf MVVM and editing DatagridCell



In an MVVM project the row of a Datagrid is bound is bound to an object OA and a column of the datagrid is bound to property PB=Code.Value of this object.


Code.Value



Binding="{Binding Code.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"


Binding="{Binding Code.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"



After changing the property PB we do OnPropertyChanged("Code");


OnPropertyChanged("Code");



This seems to work all right, except that when I have edited the value PB in the cell of the datagrid and then click on another control outside the datagrid or on another row of the datagrid or also when I use the enter key, then the setter of property PB is triggered with a blank value "". This is triggered via "external code".


""



I noticed that when I edit the cell a second time, this does not happen ...



The source code of xaml datagrid:


<DataGrid Style="{StaticResource MyDataGrid}" BorderBrush="Black" Width="150"
BorderThickness="1" HorizontalScrollBarVisibility="Hidden" Grid.Row="2" Height="200"
AutoGenerateColumns="False" HorizontalAlignment="Left" ColumnHeaderHeight="24" Margin="4"
VirtualizingPanel.IsVirtualizing="true" VirtualizingPanel.VirtualizationMode="Recycling"
EnableRowVirtualization="True" EnableColumnVirtualization="True" HeadersVisibility="Column"
SelectedItem="{Binding ItemSelection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding ListItems, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
SelectionMode="Single" SelectionUnit="FullRow" GridLinesVisibility="None" SelectionChanged="Grid_SelectionChanged" >

<DataGrid.Columns>

<DataGridTextColumn CanUserSort="False" CanUserReorder="false" Header="Name" Width="*"
Binding="{Binding Code.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell" BasedOn="{StaticResource DataGridCellEdit}"/>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource dgTextBlockViewLinks}">
<Setter Property="ToolTip" Value="{Binding Code.Tooltip}"/>
<Setter Property="Background" Value="{Binding Code.Background}"/>
<Setter Property="Foreground" Value="{Binding Code.Foreground}"/>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource dgTextBoxEditLeft}">
<Setter Property="MaxLength" Value="20"/>
</Style>
</DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

</DataGrid.Columns>

</DataGrid>



Maybe someone has an intuition of what is going on here.
I would very much like to prevent using canceledits and/or commitedits on the cell.





what do you mean triggered via "External Code"
– Muds
Jun 28 at 12:46





What I mean is, that after hitting Enter in the UI the code enters the setter of Code.Value with a blank value but the call stack of VS shows that it comes from External Code. When I hit enter the datagridcell shows the correct value that I entered manually via the UI, but there is a system call that updates this value to a blank value.
– Gerard
Jun 28 at 13:01


Code.Value





Maybe validation failed because datacolumn type doesn't match input type data...
– Maxwell77
Jun 28 at 16:00





either it is validation, or converter or some template that hides actual cell... share your xaml might be a little useful, also do check if source to view update works fine
– Muds
Jun 28 at 16:42





I checked and there is no validation error (also second time it works) and update source to view works fine. Added xaml code.
– Gerard
Jun 29 at 8:29









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV