有没有办法去禁用禁用的按钮上的图像去饱和?

Jie*_*eng 3 wpf triggers styles icommand

有没有办法可以在禁用的按钮中对图像进行去饱和处理?例如.ICommand.CanExecute = false?或者我需要使用单独的图像+样式/触发器

Hei*_*nzi 6

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

<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>
Run Code Online (Sandbox Code Playgroud)


Joh*_*son 5

这是一个降低饱和度的着色器(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);
}
Run Code Online (Sandbox Code Playgroud)

编译它,并将.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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以像这样在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>
Run Code Online (Sandbox Code Playgroud)