簡體   English   中英

如何從另一個資源設置 WPF 按鈕 TextBlock 樣式?

[英]How to set WPF Button TextBlock style from another resource?

我在 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>

但是,我希望ContentPresenterTextBlock使用如下定義的命名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<Button> ,並且樣式定義可以放在( UserControlWindow )資源字典中。

我怎樣才能做到這一點 ?

通過直接分配的簡單解決方案

您可以通過將TextBlock指定為每個Button Content來直接設置樣式。

<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定義上面的數據模板。 然后,如果Contentstring ,則ContentPresenter將僅應用它。 對於所有其他內容類型,它將照常工作。

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM