Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于更新嵌套子元素样式的WPF样式触发器_C#_.net_Wpf - Fatal编程技术网

C# 用于更新嵌套子元素样式的WPF样式触发器

C# 用于更新嵌套子元素样式的WPF样式触发器,c#,.net,wpf,C#,.net,Wpf,我有一个带有图标的按钮,我正在尝试设置一个按钮的样式,这样当它被禁用时,图标颜色也会变灰。我厌倦了使用样式触发器的几种不同方式,但我无法让它工作。我是WPF造型的新手,因此非常感谢您的帮助。多谢各位 按钮代码: <Button x:Name="btnNewProperty" Click="btnNewProperty_Click" Style="{StaticResource iconButton}" Width="145"> <StackPanel>

我有一个带有图标的按钮,我正在尝试设置一个按钮的样式,这样当它被禁用时,图标颜色也会变灰。我厌倦了使用样式触发器的几种不同方式,但我无法让它工作。我是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>

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

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


我将您的代码粘贴到VS 2013中,并注意到在您的样式中,模板的Setter具有TargetType路径。这是不允许的,XAML编辑器会显示错误。按钮的控件模板设置不正确。下面是一个链接,指向一个自定义按钮的示例,它允许您获得按钮模板行为的好处:

用按钮尝试这种样式

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

新财产