WPF样式触发器更新嵌套的子元素样式

时间:2016-05-04 21:10:30

标签: c# .net wpf

我有一个带图标的按钮,我试图设置按钮的样式,因此当它被禁用时,图标颜色也会变灰。我使用样式触发器累了几种不同的方法,但我无法让它运行起来。我对WPF样式很陌生,所以任何帮助都会非常感激。谢谢。

按钮代码:

<Button x:Name="btnNewProperty" Click="btnNewProperty_Click" Style="{StaticResource iconButton}" Width="145">
    <StackPanel>
        <Viewbox>
            <Path Data="M17,13H13V17H11V13H7V11H11V7H13V11H17M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z" />
        </Viewbox>
        <TextBlock>New Property</TextBlock>
    </StackPanel>
</Button>

我正在使用的样式:

<Application.Resources>

<SolidColorBrush x:Key="IconColor" Color="#FF444444" />
<SolidColorBrush x:Key="DisabledIconColor" Color="#FF999999" />

<Style x:Key="iconButton" TargetType="{x:Type Button}">
    <Setter Property="Margin" Value="5,5,5,5" />
    <Setter Property="Width" Value="120" />
    <Setter Property="Height" Value="25" />
    <Setter Property="FontSize" Value="14" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Path">
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Value="{StaticResource DisabledIconColor}" Property="Fill" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Resources>
        <Style TargetType="{x:Type StackPanel}">
            <Setter Property="Orientation" Value="Horizontal" />
        </Style>
        <Style TargetType="{x:Type Viewbox}">
            <Setter Property="Width" Value="16" />
            <Setter Property="Height" Value="16" />
        </Style>
        <Style TargetType="{x:Type Path}">
            <Setter Property="Margin" Value="-2,-4,0,0" />
            <Setter Property="Fill" Value="{StaticResource IconColor}" />
        </Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Margin" Value="5,-2,0,0" />
            <Setter Property="Padding" Value="0,0,0,0" />
        </Style>
    </Style.Resources>            
</Style>

</Application.Resources>

3 个答案:

答案 0 :(得分:0)

我将您的代码粘贴到VS 2013中,并在您的Style中注意到Setter for Template的TargetType为Path。这是不允许的,XAML编辑器显示错误。您的按钮控件模板设置不正确。以下链接指向自定义按钮的一个很好的示例,它允许您获得按钮模板行为的好处:http://markheath.net/post/creating-custom-wpf-button-template-in

答案 1 :(得分:0)

使用按钮

尝试此样式
<Style TargetType="Button" x:Key="MyButton">
            <Setter Property="Background" Value="#FF444444" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <StackPanel Background="{TemplateBinding Background}">
                            <Viewbox>
                                <Path Data="M17,13H13V17H11V13H7V11H11V7H13V11H17M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z" Fill="{TemplateBinding Background}"/>
                            </Viewbox>
                            <TextBlock>New Property</TextBlock>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="#FF999999"/>
                </Trigger>
            </Style.Triggers>
        </Style>

答案 2 :(得分:-1)

请在按钮样式内尝试使用此

<Button>
  <Image Source="something.png">
    <Image.Style>
      <Style TargetType="Image">
        <Style.Triggers>
          <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </Image.Style>
  </Image>
</Button>