7

リンクボタンに関するこの質問に対するこの回答に取り組んでいます:

https://stackoverflow.com/a/3564706/945

問題は、TextDecoration Underline スタイルが自動生成された TextBlocks にのみ適用されていることです。

<Button Style="{StaticResource LinkButton}">Text</Button> 

「テキスト」には下線が引かれています

<Button Style="{StaticResource LinkButton}"><TextBlock Text='Text' /></Button> 

「テキスト」には下線が引かれていません

コンテンツ内の TextBlock に適用されないのはなぜですか?

これは、スタイルの関連部分です。

<Style x:Key="LinkButton" 
       TargetType="Button"
       BasedOn="{StaticResource ResourceKey={x:Type Button}}"
       >

    <Setter Property="Width" Value="Auto"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <ContentPresenter Content="{TemplateBinding Content}" 
                                  ContentTemplate="{TemplateBinding  ContentTemplate}"
                                  VerticalAlignment="Center"
                                  >
                    <ContentPresenter.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="TextDecorations" Value="Underline" />
                        </Style>
                    </ContentPresenter.Resources>
                </ContentPresenter>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
4

3 に答える 3

3

Framework要素をContentControl内に配置すると、テンプレートは適用されないと思います。TextBlockスタイルをButtonのリソースとしても宣言すると、どちらの場合も機能します。

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
<Window.Resources>
    <Style x:Key="LinkButton"
           BasedOn="{StaticResource ResourceKey={x:Type Button}}"
           TargetType="Button">

        <Setter Property="Width" Value="Auto" />

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter VerticalAlignment="Center"
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}" >
                        <ContentPresenter.Resources>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="TextDecorations" Value="Underline" />
                            </Style>
                        </ContentPresenter.Resources>
                    </ContentPresenter>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Grid x:Name="LayoutRoot">
    <StackPanel>
        <Button Style="{StaticResource LinkButton}">Text</Button>
        <Button Style="{StaticResource LinkButton}">
            <Button.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="TextDecorations" Value="Underline" />
                </Style>
            </Button.Resources>
            <TextBlock Text="Text" />
        </Button>
    </StackPanel>
</Grid>
</Window>
于 2012-11-29T15:36:41.810 に答える
2

昨日、似たようなトラブルに遭遇しました。テキスト ブロックのスタイル セッターをテンプレート ノードの外に移動できるので、コントロールのコンテンツを変更した後にスタイルが消去されません。

于 2013-10-15T10:08:53.237 に答える
0

問題は、設定することですx:Key="LinkButton"。キーを設定すると、暗黙的なスタイルは機能しません。キーを削除すると、すべてのボタンに適用されます

于 2016-01-22T12:24:32.943 に答える