2

WPFアプリケーションには、一貫した方法でエラーを表示するために使用する共通の制御テンプレートがあります

<ResourceDictionary>
    <ControlTemplate x:Key="ErrorTemplate">
        <Border BorderThickness="1" BorderBrush="Red">
            <AdornedElementPlaceholder />
        </Border>
    </ControlTemplate>
</ResourceDictionary>

アプリケーションの他の場所で、コントロールにエラーが表示される可能性がある場合は、ErrorTemplateを次のように設定します。

<TextBox Validation.ErrorTemplate="{DynamicResource ErrorTemplate}" />

このエラーテンプレートにツールチップを表示したいのですが、境界線にツールチッププロパティを設定しても、ユーザーが1px幅の境界線上にマウスを置いたときにのみツールチップが表示され、エラーのあるコントロール自体では表示されないため、あまり役に立ちません。 。

ツールチップをスタイルに設定できることは知っていますが、このエラーテンプレートは多くの異なるコントロール(コンボボックスなど)に適用され、これらのコントロールの多くはエラーテンプレートから独立したスタイルも使用します-本当にしたいエラーテンプレートを一般的な方法で任意のコントロールに適用できます。

ErrorTemplateでツールチップを設定する方法はありますか?

4

3 に答える 3

1

スタイルが定義されています。たとえば、テキストボックスにデータバインドされているプロパティ(LastName)の検証を行うオブジェクト(Customer)にIDataErrorInfoがあります。これが私のスタイルです:

<Style x:Key="ValidationTextBox" TargetType="{x:Type Control}">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Margin" Value="0,2,40,2"/>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <Border Background="#B22222" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                                    ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                                <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White"/>
                            </Border>
                            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
                                <Border BorderBrush="#B22222" BorderThickness="1" />
                            </AdornedElementPlaceholder>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style

<TextBox Style="{StaticResource ValidationTextBox}" Text="{Binding Path=Customer.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />

于 2013-02-27T17:59:15.583 に答える
0

ここでの回答で述べたように、次のことができます。

<ControlTemplate x:Key="ErrorTemplate">
    <Border BorderThickness="1" BorderBrush="Red"
            Background="Transparent"
            ToolTip="{Binding Path=/ErrorContent}">
        <AdornedElementPlaceholder />
    </Border>
</ControlTemplate>
于 2013-02-27T18:42:51.823 に答える
0

昨日は時間がなくてすみません...以下を試して、これがあなたが求めているものかどうかを確認してください.

<Style x:Key="ValidationTextBox2" TargetType="{x:Type Control}">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderBrush="Red" BorderThickness="2">
                            <DockPanel LastChildFill="True" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" Background="Transparent">
                                <TextBlock />
                            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
                            </AdornedElementPlaceholder>
                        </DockPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
于 2013-03-01T13:49:21.557 に答える