在MVVM项目中,已绑定Datagrid的行绑定到对象OA,而该Datagrid的列绑定到此对象的属性PB = {Code.Value
}。
Binding="{Binding Code.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, ValidatesOnDataErrors=True}"
更改属性PB后,我们进行OnPropertyChanged("Code");
这似乎可以正常工作,除了当我在数据网格的单元格中编辑了值PB,然后单击数据网格之外的另一个控件或数据网格的另一行时,或者当我使用Enter键时,然后使用空白值""
触发属性PB的设置器。这是通过“外部代码”触发的。
我注意到,当我第二次编辑单元格时,这不会发生...
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>
也许有人对这里发生的事情有直觉。
我非常想避免在单元格上使用canceledits和/或commitedits。