如果Datagrid / DataGridCells中存在ValidationErrors,则在获取信息时遇到问题。
我想要做的是通过命令 (CanExecute)基于验证错误的存在或不存在来禁用按钮。 因此,我将DataGrid的Validation.HasError绑定到Button上的CommandParameter。
验证是通过ViewModel中的 IDataErrorInfo实现的,并且工作得很好。 任何包含错误值的DataGridCell都会获得一个红色边框和一个描述错误的工具提示。
我只是无法工作是将Button的CommandParameter绑定到DataGrid上的Validation.HasError。 如果我调试此问题,Validation.HasError始终为false。 为什么? 我该如何解决它?
我尝试了几乎所有在网上和其他地方找到的“解决方案”。 目前为止没有任何工作
我的DataGrid XAML:
<DataGrid x:Uid="DataGrid_1" Name="SomeDataGrid" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" ItemsSource="{Binding SomeItems}" SelectedItem="{Binding SomeSelectedItem, Mode=TwoWay}" CanUserSortColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="False" IsSynchronizedWithCurrentItem="True" IsTabStop="True" IsTextSearchEnabled="True" > <DataGrid.Resources> <SolidColorBrush x:Uid="SolidColorBrush_1" x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}" /> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn x:Uid="Comlumn1" x:Name="Comlumn1" Header="SomeHeader" Width="auto"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate x:Uid="DataTemplate_1"> <ComboBox x:Uid="ComboBox_7" ItemsSource="{Binding DataContext.Attributes,Source={StaticResource ProxyElement}}" SelectedItem="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" DisplayMemberPath="DESCRIPTION" IsEditable="False" IsTextSearchEnabled="False" Margin="0" Padding="0" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate x:Uid="DataTemplate_2"> <TextBlock x:Uid="TextBlock_15" Text="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ToolTip="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_2" Header="Value" Width="auto" Binding="{Binding VALUE, ValidatesOnDataErrors=True}"> <DataGridTextColumn.ElementStyle> <Style x:Uid="Style_4" TargetType="{x:Type TextBlock}"> <Setter x:Uid="Setter_4" Property="DataGridCell.ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_3" Header="Unit" Width="auto" Binding="{Binding UNIT, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" IsReadOnly="True" /> <DataGridTextColumn x:Uid="DataGridTextColumn_4" Header="Remark" Width="auto" Binding="{Binding REMARK}" /> </DataGrid.Columns> </DataGrid>我想绑定到DataGrid Validation.Errors的按钮:
<Button x:Uid="Button_1" Content=" + " Command="{Binding AddItemCommand}" CommandParameter="{Binding (Validation.HasError), ElementName=SomeDataGrid}" />I have some trouble getting the Information if there are ValidationErrors inside the Datagrid / the DataGridCells.
What I'm trying to do is to Disable a Button via Commands (CanExecute) basedOn the presence or absence of Validation Errors. Therefore I bind the the DataGrid's Validation.HasError to the CommandParameter on the Button.
The Validation is implemented with IDataErrorInfo in the ViewModel and works just fine. Any DataGridCell containing a wrong value gets a red Border and a Tooltip describing the Error.
What I just cant get to work is Binding that Button's CommandParameter to Validation.HasError on the DataGrid. If i debug this issue, Validation.HasError is always false. Why? And how can i fix it?
I tried virtually every "solution" I found here and elsewhere on the net. Nothing worked so far.
My DataGrid XAML:
<DataGrid x:Uid="DataGrid_1" Name="SomeDataGrid" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" ItemsSource="{Binding SomeItems}" SelectedItem="{Binding SomeSelectedItem, Mode=TwoWay}" CanUserSortColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="False" IsSynchronizedWithCurrentItem="True" IsTabStop="True" IsTextSearchEnabled="True" > <DataGrid.Resources> <SolidColorBrush x:Uid="SolidColorBrush_1" x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}" /> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn x:Uid="Comlumn1" x:Name="Comlumn1" Header="SomeHeader" Width="auto"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate x:Uid="DataTemplate_1"> <ComboBox x:Uid="ComboBox_7" ItemsSource="{Binding DataContext.Attributes,Source={StaticResource ProxyElement}}" SelectedItem="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" DisplayMemberPath="DESCRIPTION" IsEditable="False" IsTextSearchEnabled="False" Margin="0" Padding="0" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate x:Uid="DataTemplate_2"> <TextBlock x:Uid="TextBlock_15" Text="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ToolTip="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_2" Header="Value" Width="auto" Binding="{Binding VALUE, ValidatesOnDataErrors=True}"> <DataGridTextColumn.ElementStyle> <Style x:Uid="Style_4" TargetType="{x:Type TextBlock}"> <Setter x:Uid="Setter_4" Property="DataGridCell.ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_3" Header="Unit" Width="auto" Binding="{Binding UNIT, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" IsReadOnly="True" /> <DataGridTextColumn x:Uid="DataGridTextColumn_4" Header="Remark" Width="auto" Binding="{Binding REMARK}" /> </DataGrid.Columns> </DataGrid>The Button I want to Bind to the DataGrid Validation.Errors:
<Button x:Uid="Button_1" Content=" + " Command="{Binding AddItemCommand}" CommandParameter="{Binding (Validation.HasError), ElementName=SomeDataGrid}" />最满意答案
好吧,我终于搞定了! 下面的方法确实可行,但只有在包含我的Datagrid的Window 完全加载后 (例如,在Window / Usercontrol加载的EventHandler中):
public bool IsValid(DependencyObject parent) { if (Validation.GetHasError(parent)) return false; // Validate all the bindings on the children for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (!IsValid(child)) { return false; } } return true; }Ok i finally worked it out! The following Method from here does indeed work, but only after the Window containing my Datagrid is fully loaded (eg. in the Window / Usercontrol Loaded EventHandler):
public bool IsValid(DependencyObject parent) { if (Validation.GetHasError(parent)) return false; // Validate all the bindings on the children for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (!IsValid(child)) { return false; } } return true; }WPF DataGrid Validation.HasError始终为false(MVVM)(WPF DataGrid Validation.HasError is always false (MVVM))如果Datagrid / DataGridCells中存在ValidationErrors,则在获取信息时遇到问题。
我想要做的是通过命令 (CanExecute)基于验证错误的存在或不存在来禁用按钮。 因此,我将DataGrid的Validation.HasError绑定到Button上的CommandParameter。
验证是通过ViewModel中的 IDataErrorInfo实现的,并且工作得很好。 任何包含错误值的DataGridCell都会获得一个红色边框和一个描述错误的工具提示。
我只是无法工作是将Button的CommandParameter绑定到DataGrid上的Validation.HasError。 如果我调试此问题,Validation.HasError始终为false。 为什么? 我该如何解决它?
我尝试了几乎所有在网上和其他地方找到的“解决方案”。 目前为止没有任何工作
我的DataGrid XAML:
<DataGrid x:Uid="DataGrid_1" Name="SomeDataGrid" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" ItemsSource="{Binding SomeItems}" SelectedItem="{Binding SomeSelectedItem, Mode=TwoWay}" CanUserSortColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="False" IsSynchronizedWithCurrentItem="True" IsTabStop="True" IsTextSearchEnabled="True" > <DataGrid.Resources> <SolidColorBrush x:Uid="SolidColorBrush_1" x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}" /> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn x:Uid="Comlumn1" x:Name="Comlumn1" Header="SomeHeader" Width="auto"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate x:Uid="DataTemplate_1"> <ComboBox x:Uid="ComboBox_7" ItemsSource="{Binding DataContext.Attributes,Source={StaticResource ProxyElement}}" SelectedItem="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" DisplayMemberPath="DESCRIPTION" IsEditable="False" IsTextSearchEnabled="False" Margin="0" Padding="0" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate x:Uid="DataTemplate_2"> <TextBlock x:Uid="TextBlock_15" Text="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ToolTip="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_2" Header="Value" Width="auto" Binding="{Binding VALUE, ValidatesOnDataErrors=True}"> <DataGridTextColumn.ElementStyle> <Style x:Uid="Style_4" TargetType="{x:Type TextBlock}"> <Setter x:Uid="Setter_4" Property="DataGridCell.ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_3" Header="Unit" Width="auto" Binding="{Binding UNIT, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" IsReadOnly="True" /> <DataGridTextColumn x:Uid="DataGridTextColumn_4" Header="Remark" Width="auto" Binding="{Binding REMARK}" /> </DataGrid.Columns> </DataGrid>我想绑定到DataGrid Validation.Errors的按钮:
<Button x:Uid="Button_1" Content=" + " Command="{Binding AddItemCommand}" CommandParameter="{Binding (Validation.HasError), ElementName=SomeDataGrid}" />I have some trouble getting the Information if there are ValidationErrors inside the Datagrid / the DataGridCells.
What I'm trying to do is to Disable a Button via Commands (CanExecute) basedOn the presence or absence of Validation Errors. Therefore I bind the the DataGrid's Validation.HasError to the CommandParameter on the Button.
The Validation is implemented with IDataErrorInfo in the ViewModel and works just fine. Any DataGridCell containing a wrong value gets a red Border and a Tooltip describing the Error.
What I just cant get to work is Binding that Button's CommandParameter to Validation.HasError on the DataGrid. If i debug this issue, Validation.HasError is always false. Why? And how can i fix it?
I tried virtually every "solution" I found here and elsewhere on the net. Nothing worked so far.
My DataGrid XAML:
<DataGrid x:Uid="DataGrid_1" Name="SomeDataGrid" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" ItemsSource="{Binding SomeItems}" SelectedItem="{Binding SomeSelectedItem, Mode=TwoWay}" CanUserSortColumns="False" CanUserAddRows="False" CanUserDeleteRows="False" IsReadOnly="False" IsSynchronizedWithCurrentItem="True" IsTabStop="True" IsTextSearchEnabled="True" > <DataGrid.Resources> <SolidColorBrush x:Uid="SolidColorBrush_1" x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{x:Static SystemColors.HighlightColor}" /> </DataGrid.Resources> <DataGrid.Columns> <DataGridTemplateColumn x:Uid="Comlumn1" x:Name="Comlumn1" Header="SomeHeader" Width="auto"> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate x:Uid="DataTemplate_1"> <ComboBox x:Uid="ComboBox_7" ItemsSource="{Binding DataContext.Attributes,Source={StaticResource ProxyElement}}" SelectedItem="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" DisplayMemberPath="DESCRIPTION" IsEditable="False" IsTextSearchEnabled="False" Margin="0" Padding="0" /> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate x:Uid="DataTemplate_2"> <TextBlock x:Uid="TextBlock_15" Text="{Binding Attribute, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" ToolTip="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_2" Header="Value" Width="auto" Binding="{Binding VALUE, ValidatesOnDataErrors=True}"> <DataGridTextColumn.ElementStyle> <Style x:Uid="Style_4" TargetType="{x:Type TextBlock}"> <Setter x:Uid="Setter_4" Property="DataGridCell.ToolTip" Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" /> </Style> </DataGridTextColumn.ElementStyle> </DataGridTextColumn> <DataGridTextColumn x:Uid="DataGridTextColumn_3" Header="Unit" Width="auto" Binding="{Binding UNIT, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" IsReadOnly="True" /> <DataGridTextColumn x:Uid="DataGridTextColumn_4" Header="Remark" Width="auto" Binding="{Binding REMARK}" /> </DataGrid.Columns> </DataGrid>The Button I want to Bind to the DataGrid Validation.Errors:
<Button x:Uid="Button_1" Content=" + " Command="{Binding AddItemCommand}" CommandParameter="{Binding (Validation.HasError), ElementName=SomeDataGrid}" />最满意答案
好吧,我终于搞定了! 下面的方法确实可行,但只有在包含我的Datagrid的Window 完全加载后 (例如,在Window / Usercontrol加载的EventHandler中):
public bool IsValid(DependencyObject parent) { if (Validation.GetHasError(parent)) return false; // Validate all the bindings on the children for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (!IsValid(child)) { return false; } } return true; }Ok i finally worked it out! The following Method from here does indeed work, but only after the Window containing my Datagrid is fully loaded (eg. in the Window / Usercontrol Loaded EventHandler):
public bool IsValid(DependencyObject parent) { if (Validation.GetHasError(parent)) return false; // Validate all the bindings on the children for (int i = 0; i != VisualTreeHelper.GetChildrenCount(parent); ++i) { DependencyObject child = VisualTreeHelper.GetChild(parent, i); if (!IsValid(child)) { return false; } } return true; }
发布评论