有没有办法降低禁用按钮上图像的饱和度?

发布于 2024-10-04 23:43:06 字数 92 浏览 0 评论 0原文

有没有办法可以降低禁用按钮中图像的饱和度?例如。 ICommand.CanExecute = false?或者我需要使用单独的图像+样式/触发器

Is there a way I can desaturate images in Buttons that are disabled? eg. ICommand.CanExecute = false? or do I need to use separate images + Style/Triggers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-10-11 23:43:06

这是一个去饱和的着色器(Desaturate.fx):

sampler2D  inputSampler : register(S0);
/// <summary>The strength of the effect.</summary>
/// <minValue>0</minValue>
/// <maxValue>1</maxValue>
/// <defaultValue>0</defaultValue>
float Strength : register(C0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 srcColor = tex2D(inputSampler, uv);
    float3 rgb = srcColor.rgb;
    float3 c = (1 - Strength)*rgb + Strength* dot(rgb, float3(0.30, 0.59, 0.11));
    return float4(c, srcColor.a);
}

编译它并添加 .ps 文件作为资源,然后像这样包装它:

public class Desaturate : ShaderEffect
{
    public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty(
        "Input",
        typeof(Desaturate),
        0);

    public static readonly DependencyProperty StrengthProperty = DependencyProperty.Register(
        "Strength",
        typeof(double),
        typeof(Desaturate),
        new UIPropertyMetadata(((double)(0D)), PixelShaderConstantCallback(0)));

    public Desaturate()
    {
        PixelShader pixelShader = new PixelShader();
        pixelShader.UriSource = new Uri("/So.Wpf;component/Effects/Desaturate.ps", UriKind.Relative);
        this.PixelShader = pixelShader;

        this.UpdateShaderValue(InputProperty);
        this.UpdateShaderValue(StrengthProperty);
    }
    public Brush Input
    {
        get
        {
            return ((Brush)(this.GetValue(InputProperty)));
        }
        set
        {
            this.SetValue(InputProperty, value);
        }
    }
    /// <summary>The strength of the effect. 0 is unchanged and 1 is monochrome</summary>
    public double Strength
    {
        get
        {
            return ((double)(this.GetValue(StrengthProperty)));
        }
        set
        {
            this.SetValue(StrengthProperty, value);
        }
    }
}

然后您可以像这样在 Xaml 中使用它,强度是可绑定的,因此 np 将其连接到通过触发器启用:

<Grid>
    <Image Source="http://i.imgur.com/CN63KcN.jpg">
        <Image.Effect>
            <effects:Desaturate Strength="{Binding ElementName=SaturationSlider, Path=Value}"/>
        </Image.Effect>
    </Image>
    <Slider x:Name="SaturationSlider" Minimum="0" Maximum="1" VerticalAlignment="Bottom"/>
</Grid>

Here is a shader that desaturates (Desaturate.fx):

sampler2D  inputSampler : register(S0);
/// <summary>The strength of the effect.</summary>
/// <minValue>0</minValue>
/// <maxValue>1</maxValue>
/// <defaultValue>0</defaultValue>
float Strength : register(C0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 srcColor = tex2D(inputSampler, uv);
    float3 rgb = srcColor.rgb;
    float3 c = (1 - Strength)*rgb + Strength* dot(rgb, float3(0.30, 0.59, 0.11));
    return float4(c, srcColor.a);
}

Compile it and add the .ps file as a resource and then wrap it like this:

public class Desaturate : ShaderEffect
{
    public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty(
        "Input",
        typeof(Desaturate),
        0);

    public static readonly DependencyProperty StrengthProperty = DependencyProperty.Register(
        "Strength",
        typeof(double),
        typeof(Desaturate),
        new UIPropertyMetadata(((double)(0D)), PixelShaderConstantCallback(0)));

    public Desaturate()
    {
        PixelShader pixelShader = new PixelShader();
        pixelShader.UriSource = new Uri("/So.Wpf;component/Effects/Desaturate.ps", UriKind.Relative);
        this.PixelShader = pixelShader;

        this.UpdateShaderValue(InputProperty);
        this.UpdateShaderValue(StrengthProperty);
    }
    public Brush Input
    {
        get
        {
            return ((Brush)(this.GetValue(InputProperty)));
        }
        set
        {
            this.SetValue(InputProperty, value);
        }
    }
    /// <summary>The strength of the effect. 0 is unchanged and 1 is monochrome</summary>
    public double Strength
    {
        get
        {
            return ((double)(this.GetValue(StrengthProperty)));
        }
        set
        {
            this.SetValue(StrengthProperty, value);
        }
    }
}

Then you can use it in Xaml like this, the strength is bindable so np hooking it up to enabled via trigger:

<Grid>
    <Image Source="http://i.imgur.com/CN63KcN.jpg">
        <Image.Effect>
            <effects:Desaturate Strength="{Binding ElementName=SaturationSlider, Path=Value}"/>
        </Image.Effect>
    </Image>
    <Slider x:Name="SaturationSlider" Minimum="0" Maximum="1" VerticalAlignment="Bottom"/>
</Grid>
虐人心 2024-10-11 23:43:06

我为此使用了一种特殊的样式,当按钮被禁用时,它会降低图像的不透明度(是的,如果按钮绑定到命令,这也有效)。从技术上讲,这不是去饱和,但它看起来很相似,它可能会帮助您自己得出解决方案:

<Style x:Key="buttonImage">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
            <Setter Property="Image.Opacity" Value="0.25"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

I'm using a special style for this, which reduces the opacity of the image when the button gets disabled (yes, this also works if the button is bound to a command). Technically, this is not desaturation, but it looks similar and it might help you derive a solution on your own:

<Style x:Key="buttonImage">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}, AncestorLevel=1}, Path=IsEnabled}" Value="False">
            <Setter Property="Image.Opacity" Value="0.25"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文