我正在尝试验证树视图中的项目。主要思想是用户从树中选择一个对象并加载其可以编辑的详细信息。这一切都很好,因为我已经INotifyPropertyChanged
连接了,但是......我的模型已经连接了验证逻辑(IDataErrorInfo
),我也想在树视图中显示它(突出显示有验证错误的项目)。
我已经尝试了一些事情,但我只是不知道将验证放在绑定中的哪个位置以使其像我希望的那样工作。
我的验证控制模板:
<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>
树视图:
<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>
基本上,如果基础模型有验证错误,我会尝试在树视图中的项目旁边放置一个小图标。
我尝试过使用 BindingGroup ,但这对我来说是一个新话题,所以我不确定这是否是可行的方法。
有任何想法吗?