减轻的背景颜色上按一下按钮与每个转换器结合 [英] lighten background color on button click per binding with converter

查看:121
本文介绍了减轻的背景颜色上按一下按钮与每个转换器结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要减轻点击一个按钮的背景。所以,我做了以下内容:

I want to lighten a buttons background on click. So I did the following:

<converter:ColorLightConverter x:Key="colorLightConverter" />

...

<Style BasedOn="{StaticResource default}" TargetType="{x:Type controls:Button}">            
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type controls:Button}">
                    <ControlTemplate.Triggers>                            
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="{Binding Path=Background.Color, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource colorLightConverter}}" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="Transparent"
                            BorderThickness="0">
                        ...                            
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

转换器:

class ColorLightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color color = (Color)value;
            System.Drawing.Color lightColor = ControlPaint.Light(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B));

            return Color.FromArgb(lightColor.A, lightColor.R, lightColor.G, lightColor.B);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

但是,当我点击按钮转换器不叫。我觉得有什么不对的约束力,但我不能看到的错误...

But the converter isn't called when I click the button. I think there is anything wrong with the binding, but I can't see the error...

你能帮助我吗?

也许我是完全错误的。我基本上要做到:当点击该按钮,以当前的背景色,淡化它。没有更多...

Maybe I'm completely wrong. What I basically want to do: When clicking the button, take the current background color and lighten it. Not more...

我试过如下:

更改了绑定了一下:

<Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="{Binding Path=Background.Color, RelativeSource={RelativeSource Self}, Converter={StaticResource colorLightConverter}}" />
                        </Trigger>

改变了转换器(现在它返回的SolidColorBrush):

class ColorLightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Color color = (Color)value;
            System.Drawing.Color lightColor = ControlPaint.Light(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B));

            return new SolidColorBrush(Color.FromArgb(lightColor.A, lightColor.R, lightColor.G, lightColor.B));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

现在的转换器被调用,但它被再次调用并再次,因此计算器异常几秒钟后抛出。其中,来自于这个无限循环?我真的有点此刻迷茫......

Now the converter is called, but it is called again and again, so a stackoverflow exception is thrown after a few seconds. Where comes this indefinite loop from? I'm really a bit confused at the moment...

推荐答案

在WPF你不应该使用的System.Drawing.Color。使用System.Windows.Media.Color代替。另一个重要的事情是,ControlTemplate.Triggers里面塞特斯不必使用的RelativeSource TemplatedParent引用控制。刚:

You shouldn't be using System.Drawing.Color on WPF. Use System.Windows.Media.Color instead. Another important thing is that the Setters inside the ControlTemplate.Triggers do not have to reference the control by using RelativeSource TemplatedParent. Just:

<Trigger Property="IsPressed" Value="True">
    <Setter Property="Background" Value="YourLightColor"/>
</Trigger>

和保持休息一样。现在,如果你需要指定自定义逻辑(如转换器,只需将转换器在二传手。

And keep the rest the same. Now, if you need to specify custom logic (such as a converter, just place the converter in that setter.

编辑:
无限循环来自于你绑定到Background.Color,然后设置背景,这将触发一个属性更改通知到WPF产权制度,这反过来又导致绑定被刷新,所以这样的事实...我想你也许能够解决这个通过设置结合模式,以一次性=

The infinite loop comes from the fact that you're Binding to Background.Color, and then Setting Background, which triggers a Property Change notification into the WPF Property System, which in turn causes the Binding to be refreshed, and so on... I think you might be able to work around this by setting the binding Mode to = OneTime

这篇关于减轻的背景颜色上按一下按钮与每个转换器结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆