【问题标题】:How to set WPF Button TextBlock style from another resource?如何从另一个资源设置 WPF 按钮 TextBlock 样式?
【发布时间】:2020-10-01 19:29:16
【问题描述】:

我在 WPF 应用程序中设置按钮的样式:

<Style TargetType="{x:Type Button}">
    <Setter Property="ToolTipService.InitialShowDelay" Value="0"/>
    <Setter Property="Height" Value="22" />
    <Setter Property="Margin" Value="1" />
    <Setter Property="Padding" Value="0" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Padding="{TemplateBinding Padding}"
                        BorderThickness="1">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1 -1 1 0"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Background" Value="{StaticResource NotSelectedButton}" />
    <Setter Property="Foreground" Value="{StaticResource ActiveFg}" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="{StaticResource Selected}" />
            <Setter Property="Foreground" Value="{StaticResource SelectedFg}" />
        </Trigger>
        <Trigger Property="IsMouseOver" Value="False">
            <Setter Property="Background" Value="{StaticResource NotSelectedButton}" />
            <Setter Property="Foreground" Value="{StaticResource ActiveFg}" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="{StaticResource NotSelectedButton}" />
            <Setter Property="Foreground" Value="{StaticResource InactiveFg}" />
            <!--<Setter Property="Foreground" Value="{StaticResource NotSelected}" />-->
        </Trigger>
    </Style.Triggers>
</Style>

但是我希望ContentPresenter 中的TextBlock 使用这样定义的命名TextBlock 样式:

<Style x:Key="ButtonText" TargetType="{x:Type TextBlock}">
    <Setter Property="ToolTipService.InitialShowDelay" Value="0"/>
    <Setter Property="Width" Value="auto" />
    <Setter Property="MinWidth" Value="20" />
    <Setter Property="TextAlignment" Value="Left" />
    <Setter Property="Padding" Value="0 0 0 0" />
    <Setter Property="Margin" Value="1 0 0 0" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="HorizontalAlignment" Value="Center" />
</Style>

要测试这种风格,只需 UserControlWindow 中的 Button 就足够了:&lt;Button&gt;,并且风格定义可以进入资源字典(UserControlWindow)。

我该怎么做?

【问题讨论】:

    标签: wpf xaml controltemplate


    【解决方案1】:

    通过直接赋值的简单解决方案

    您可以通过将TextBlock 分配为每个ContentContent 来直接设置样式。

    <Button>
       <TextBlock Text="Test" Style="{StaticResource ButtonText}"/>
    </Button>
    

    仅支持文本作为内容

    如果您确定只将字符串作为内容放入按钮中,则可以将包含TextBlockContentTemplate 分配给ContentPresenter。这不适用于其他内容类型,因为它不会像往常一样显示它们,而只是在TextBlock 中显示它们的类型名称。

    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1 -1 1 0">
       <ContentPresenter.ContentTemplate>
          <DataTemplate>
             <TextBlock Style="{StaticResource ButtonText}" Text="{Binding}"/>
          </DataTemplate>
       </ContentPresenter.ContentTemplate>
    </ContentPresenter>
    

    支持所有内容类型

    如果您想支持任何内容类型作为按钮的内容,您可以在ContentPresenter.Resources 中定义上面的数据模板。然后,ContentPresenter 将只应用它,如果Contentstring。对于所有其他内容类型,它将照常工作。

    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1 -1 1 0">
       <ContentPresenter.Resources>
          <DataTemplate DataType="{x:Type system:String}">
             <TextBlock Style="{StaticResource ButtonText}" Text="{Binding}"/>
          </DataTemplate>
       </ContentPresenter.Resources>
    </ContentPresenter>
    

    【讨论】:

    • ContentPresenter.Resources 的最后一个选项是完美的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多