0

TextBox の単純な Validation.ErrorTemplate を次のように宣言しました。

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock Text="!" DockPanel.Dock="Right" 
                               FontSize="{TemplateBinding TextBox.FontSize}" 
                               Foreground="Red"/>
                    <AdornedElementPlaceholder  Name="adornerPlaceholder" />
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

感嘆符のフォント サイズは TextBox と同じフォント (編集)サイズになることを期待していますが、期待どおりの結果にはならず、常に既定のフォント サイズが取得されます。さらに、 を使って Binding を試みRelativeSource={RelativeSource Mode=TemplatedParent}, Path=FontSizeましたが、これも解決できません。なぜこのような状況が発生したのですか?感嘆符を TextBox と同じサイズにするにはどうすればよいですか?

4

2 に答える 2

1

にバインドしませんAdornedElementPlaceholderか?

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <DockPanel LastChildFill="True">
                <TextBlock Text="!" DockPanel.Dock="Right" 
                           FontSize="{Binding ElementName=adornerPlaceholder, Path=AdornedElement.FontSize}" 
                           Foreground="Red"/>
                <AdornedElementPlaceholder  Name="adornerPlaceholder" />
            </DockPanel>
        </ControlTemplate>
    </Setter.Value>
</Setter>
</Style>

これはテストされていませんが、動作するはずです:)

于 2011-09-26T13:23:19.970 に答える
0

別のオプションは、 を でラップすることですTextBlockViewboxこれにより、装飾された要素とともに高さが自動的にスケーリングされます。

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <Viewbox DockPanel.Dock="Right" 
                        Height="{Binding ElementName=adornerPlaceholder, Path=ActualHeight}" 
                        Stretch="Uniform"
                        Margin="5 0">
                        <TextBlock Text="!" Foreground="Red" />
                    </Viewbox>
                    <AdornedElementPlaceholder Name="adornerPlaceholder" />
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

これは、フォントサイズ、感嘆符のグラフィック(テキスト、パス、要素など)に関係なく、装飾されている要素に対して機能します。

配置/レイアウトは余裕を持って微調整できます。

于 2011-09-26T13:29:57.757 に答える