TextBox と ComboBox を含む複数のコントロールがあり、Validation.Errors コレクションに含まれるすべてのエラーを含むツールヒントをすべてのコントロールに表示したいと考えています。できれば全員に共通のスタイルを共有してもらいたいと思っており、それが私が試みていることです。ToolTip セッターのバインディングに何か問題があると確信していますが、何が原因かわかりません。エラーの重大度 (エラーまたは警告) を指定する INotifyDataErrorInfo 実装で Error オブジェクトを返します。
ウィンドウ内のすべてのコントロールに適用され、そのコントロールのすべてのエラーと警告のリストを含むツールヒントを表示するスタイルが必要です。エラーは赤で、警告は黄色で表示されます。これが私が思いついたスタイルです:
<Style TargetType="FrameworkElement">
<Setter Property="ToolTip">
<Setter.Value>
<ItemsControl ItemsSource="{Binding Path=(Validation.Errors), RelativeSource={RelativeSource Self}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent.ErrorMessage}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ErrorContent.ErrorSeverity}"
Value="{x:Static local:ErrorType.Warning}">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Setter.Value>
</Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=(Validation.HasError)}" Value="True">
<Setter Property="ToolTip">
<Setter.Value>
<ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent.ErrorMessage}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ErrorContent.ErrorSeverity}"
Value="{x:Static local:ErrorType.Warning}">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
RelativeSource を変更して、AncestorLevel 1 と 2 の両方で Control の AncestoryType を検索しようとしましたが、どれもうまくいかないようです。
ほとんど同じことを行う ErrorTemplate に使用した ControlTemplate に基づいてスタイルを作成しました。エラーの重大度に応じて赤または黄色の境界線を表示し、コントロールの ToolTip に対してやりたいこととまったく同じように ToolTip を表示します。自体。ErrorTemplate の DataContext が自動的に Validation.Errors コレクションに設定され、ItemsSource を ItmesCollection にバインドしやすくなるため、バインディングに何らかの関係があると確信しています。スタイルのツールチップにはそのような運はありません。これが、ErrorTemplate に使用した実際の ControlTemplate です。
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderThickness="1">
<AdornedElementPlaceholder Name="ElementPlaceholder"/>
<Border.Style>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ElementPlaceholder, Path=AdornedElement.(Validation.Errors)[0].ErrorContent.ErrorSeverity}"
Value="{x:Static local:ErrorType.Warning}">
<Setter Property="BorderBrush" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<Border.ToolTip>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ErrorContent.ErrorMessage}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ErrorContent.ErrorSeverity}"
Value="{x:Static local:ErrorType.Warning}">
<Setter Property="Foreground" Value="Yellow"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border.ToolTip>
</Border>
</ControlTemplate>
誰でも私に何か提案をしてもらえますか?