“变灰”WPF 按钮图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2530912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:37:36  来源:igfitidea点击:

'Graying Out' a WPF button image?

wpfwpf-controls

提问by David Veeneman

I have a simple Buttoncontrol that contains an Imageobject as its content. I want so set the Imageopacity to 0.5 when the Buttonis disabled to provide an additional visual cue as to the Buttonstatus.

我有一个简单的Button控件,其中包含一个Image对象作为其内容。我想ImageButton禁用时将不透明度设置为 0.5,以提供有关Button状态的额外视觉提示。

What is the simplest way to accomplish that result in XAML? Thanks for your help.

在 XAML 中实现该结果的最简单方法是什么?谢谢你的帮助。

回答by itowlson

Use a trigger in the Image style. (It would be more natural to put it in the Button style, but the Button style can't easily affect the Image for annoying technical reasons. It couldbe done in the Button's ControlTemplate but that's overkill for what you want here.)

在图像样式中使用触发器。(将它放在 Button 样式中会更自然,但由于烦人的技术原因,Button 样式不能轻易影响 Image。它可以在 Button 的 ControlTemplate 中完成,但这对于您在这里想要的东西来说太过分了。)

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

Note we are taking advantage here of the fact that the Image will be disabled when the Button is disabled, so we can trigger directly on the Image's own IsEnabled property. In other cases, the Button property we want to trigger on might not be inherited by the Image; in that case, we'd need to use a DataTrigger with the FindAncestor RelativeSource to bind to the containing button.

请注意,我们在这里利用了当 Button 被禁用时 Image 将被禁用的事实,因此我们可以直接在 Image 自己的 IsEnabled 属性上触发。在其他情况下,我们想要触发的 Button 属性可能不会被 Image 继承;在这种情况下,我们需要使用带有 FindAncestor RelativeSource 的 DataTrigger 来绑定到包含按钮。

回答by CGsoldier

If you want something more generic, put this in your resources section for your window or UserControl .

如果你想要更通用的东西,把它放在你的窗口或 UserControl 的资源部分。

<UserControl.Resources>     
    <Style x:Key="ImageEnabled" TargetType="Image">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Opacity" Value="0.25"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

And in the actual button just do this

在实际按钮中,只需执行此操作

<Button>
    <Image Source="{StaticResource LinkImage}" Style="{StaticResource ImageEnabled}"/>
</Button>

回答by Tower

Here's a more generic style you can apply:

这是您可以应用的更通用的样式:

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

回答by voluntier

If you want the entire button greyed out, styling the button itself has worked for me. It seems to grey out all button content.

如果你想让整个按钮变灰,按钮本身的样式对我有用。它似乎使所有按钮内容变灰。

                    <Button.Style>
                        <Style TargetType="Button">
                            <Style.Triggers>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Opacity" Value=".75"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>