在 wpf 中绑定颜色的 R G B 属性 [英] Binding R G B properties of color in wpf
问题描述
我有一个自定义类FavoriteColor",它具有三个属性,R、G 和 B.现在我想绘制一个矩形并用这些 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>
它说 Color 类的属性 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.
首先,Window1 的 xaml.我们将设置三个 Sliders 并通过 SolidColorBrush 将它们的值绑定到 Window 的 Background 属性.
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>
接下来是转换器.请注意,我在这里没有进行任何错误检查 - 您确实应该检查 values
数组的长度是否为 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
}
就是这样!不需要其他代码隐藏.
That's it! No other code-behind is necessary.
这篇关于在 wpf 中绑定颜色的 R G B 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!