gpt4 book ai didi

c# - 编辑后未清除 WPF Datagrid 行验证错误

转载 作者:太空狗 更新时间:2023-10-29 21:49:42 27 4
gpt4 key购买 nike

在 WPF 中使用 DataGrid,我试图在通过 INotifyDataErrorInfo 使用错误验证时获得正确的行为。

我有一个实现该接口(interface)的类的 ObservableCollection,并将该集合绑定(bind)到 DataGrid。当出现错误时,单元格将有红色边框,行将有一个红色!在前。一切默认,一切都很好。仍在编辑时,当错误消失时,红色边框和红色!都会消失。到目前为止,一切顺利!

但是,当我离开该行(通过键盘 Enter/Tab 或使用鼠标),然后返回并删除错误时,红色单元格边框消失,但红色!留下来。

我知道之前有人提出过这个问题,例如:WPF DataGrid validation errors not clearing .但是,除了完全隐藏行验证错误之外,那里的解决方案并没有解决这个问题。 (其中,结合第二个答案 here 也很不错......)

或者我的问题是即使存在验证错误,用户也能够离开单元格的编辑模式?最好是,我想限制这一点,并在进一步编辑发生之前先强制解决错误,但我不知道如何在没有大量代码的情况下强制执行这一点......

这是 XML(RowValidationErrorTemplate 来自这里:link):

<UserControl x:Class="CustomDG"
...etc...
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
d:DataContext="{d:DesignInstance Type=viewmodels:TestViewModel}">
<Grid>
<DataGrid
ItemsSource="{Binding Path=testCollection}" AutoGenerateColumns="False"
RowHeight="18" CanUserResizeRows="False" RowHeaderWidth="18" >
<DataGrid.RowValidationErrorTemplate>
<ControlTemplate>
<Grid Margin="0,-2,0,-2"
ToolTip="{Binding RelativeSource={RelativeSource
FindAncestor, AncestorType={x:Type DataGridRow}},
Path=(Validation.Errors)[0].ErrorContent}">
<Ellipse StrokeThickness="0" Fill="Red"
Width="{TemplateBinding FontSize}"
Height="{TemplateBinding FontSize}" />
<TextBlock Text="!" FontSize="{TemplateBinding FontSize}"
FontWeight="Bold" Foreground="White"
HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</DataGrid.RowValidationErrorTemplate>-->

<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Path=Name,
ValidatesOnNotifyDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>

最佳答案

您需要在行提交或取消编辑后用空字符串引发 notifyPropertyChanged 以刷新 DataBinding From 对象,这将刷新您的界面,像这样使用它:

RaiseNotifyPropertyChanged(""); 

关于c# - 编辑后未清除 WPF Datagrid 行验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31426945/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com