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}" />
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With