我想驗證樹狀圖內的項目。主要思想是用戶從樹中選擇一個對象並加載其可編輯的細節。這一切都工作正常,因爲我有INotifyPropertyChanged
迷上了,但...我的模型有驗證邏輯掛鉤(IDataErrorInfo
),我也想在樹視圖中顯示它(突出顯示有驗證錯誤的項目)。treeview項目驗證
我已經嘗試了一些東西,但我無法弄清楚在哪裏把驗證放在綁定中,以使它像我想要的那樣工作。
我的驗證控件模板:
<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但這是一個新的話題,所以我不知道如果這是甚至要走的路。
任何想法?