在WPF结合颜色R G B性质 [英] Binding R G B properties of color in wpf

查看:188
本文介绍了在WPF结合颜色R G B性质的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义类,FavoriteColor有三个属性,研究 。现在我要画一个矩形,(使用绑定)这些R,G和B值填充它。我想在我的XAML下面的代码片段,而是给了我一个编译时错误。

I have a custom class, "FavoriteColor" that has three properties, R, G and B. Now I want to draw a rectangle and fill it with these R, G and B values (using databinding). I tried the following snippet in my xaml, but gives me a compile time error.

                <Rectangle Width="10" Height="10" Grid.Column="4">
                    <Rectangle.Fill>
                        <SolidColorBrush>
                            <SolidColorBrush.Color>
                                <Color R="{Binding Path=R}" />
                                <Color G="{Binding Path=G}" />
                                <Color B="{Binding Path=B}" />
                            </SolidColorBrush.Color>
                        </SolidColorBrush>
                    </Rectangle.Fill>
                </Rectangle>

上面说的是性能的R,G和彩色的B级不属于依赖属性。我知道,你可以将数据绑定只依赖属性,但在这种情况下,我怎么绑定我的R,G和B与矩形的填充颜色。

It says that the properties R, G and B of Color class are not dependency properties. I know that you can bind data only to dependency properties, but in this case, how do I bind my R, G and B with the rectangle's fill color.

是否有任何其他的方式比其他通过声明类型的彩色多了一个属性,然后初始化它当R,G和B都设置?也是为什么R,G和颜色类B不依赖属性?

推荐答案

让我们做到这一点使用MultiBinding和IMultiValueConverter。这里有一个完整的示例。

Let's do this using a MultiBinding and an IMultiValueConverter. Here's a full sample.

首先,XAML为窗口1。我们将建立三个滑块,并通过绑定的SolidColorBrush他们对窗口的背景属性值。

First, the xaml for Window1. We'll set up three Sliders and bind their values to the Window's Background property via a SolidColorBrush.

<Window x:Class="WpfApplication16.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:my="clr-namespace:WpfApplication16"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <my:RgbConverter x:Key="RgbConverter" />
    </Window.Resources>
    <Window.Background>
        <SolidColorBrush>
            <SolidColorBrush.Color>
                <MultiBinding Converter="{StaticResource RgbConverter}">
                    <Binding Path="Value" ElementName="redSlider" />
                    <Binding Path="Value" ElementName="greenSlider" />
                    <Binding Path="Value" ElementName="blueSlider" />
                </MultiBinding>
            </SolidColorBrush.Color>
        </SolidColorBrush>
    </Window.Background>
    <StackPanel>
        <Slider Minimum="0" Maximum="255" x:Name="redSlider" />
        <Slider Minimum="0" Maximum="255" x:Name="greenSlider" />
        <Slider Minimum="0" Maximum="255" x:Name="blueSlider" />
    </StackPanel>
</Window>

接着,将转换器。请注意,我没有做任何错误检查在这里 - 你真的应该检查数组是3和个人价值是有效的字节等的长度

Next, the converter. Note that I'm not doing any error checking here - you really should check that the length of the values array is 3 and that the individual values are valid bytes etc.

public class RgbConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var r = System.Convert.ToByte(values[0]);
        var g = System.Convert.ToByte(values[1]);
        var b = System.Convert.ToByte(values[2]);

        return Color.FromRgb(r, g, b);
    }

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

    #endregion
}

这就是它!没有其他code-背后,是必要的。

That's it! No other code-behind is necessary.

这篇关于在WPF结合颜色R G B性质的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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