I'm trying to validate items inside a treeview. The main idea is that a user selects an object from the tree and that loads its details which can be edited. That's all working fine as I've got INotifyPropertyChanged
hooked up but... my model has validation logic hooked up (IDataErrorInfo
) and I'd like to show it in the treeview as well (highlight the item(s) which have validation errors).
I've tried some things already but I just can't figure out where to put the Validation in the binding to make it work like I would like it to.
My Validation ControlTemplate:
<ControlTemplate x:Key="validationTemplate">
<StackPanel Orientation="Horizontal">
<AdornedElementPlaceholder x:Name="MyAdorner" />
<Image
MaxHeight="{Binding ElementName=MyAdorner, Path=ActualHeight}"
MaxWidth="20"
Source="{Binding Source={StaticResource ValidationIcon}, Converter={StaticResource UriConverter}}"
Margin="1" RenderOptions.BitmapScalingMode="HighQuality"
VerticalAlignment="Center" HorizontalAlignment="Center" />
</StackPanel>
</ControlTemplate>
The treeView:
<TreeView ItemsSource="{Binding Path=ProductCategories}"
Name="treeView" SelectedItemChanged="treeView_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type data:ProductCategory}"
ItemsSource="{Binding Path=ProductCategories}">
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Validation.ErrorTemplate"
Value="{StaticResource validationTemplate}" />
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<StackPanel.BindingGroup>
<BindingGroup />
</StackPanel.BindingGroup>
<StackPanel.ToolTip>
<TextBlock Margin="2" Text="{Binding Path=Description}" />
</StackPanel.ToolTip>
<TextBlock Text="{Binding Path=Name}" FontSize="10" FontWeight="Medium" />
<TextBlock Text="{Binding Path=ProductCount, StringFormat=' ({0})'}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Basically I'm trying to put a little icon next to the item in the treeview if the underlying Model has a validation error.
I've tried playing around with BindingGroup but that's a new topic for me so I'm not sure if that is even the way to go.
Any ideas?