我有一个自定义对象组合框,每个组合框均显示为彩色矩形,并在其旁边显示文本。
我有要从对象显示的ARGB值,但我想将其转换为颜色并将其设置为矩形的fill属性。到目前为止,根据这篇文章,我有以下内容 Binding R G B properties of color in wpf
如何指定要在SubCategory中分配给值转换器的财产?以及如何将转换器的结果设置为矩形的Fill属性?
// XAML
<Controls:MetroWindow.Resources>
<local:ArgbConverter x:Key="argbConverter"></local:ArgbConverter>
</Controls:MetroWindow.Resources>
<ComboBox ItemsSource="{Binding Path=SubCategories}" HorizontalAlignment="Stretch" SelectedItem="{Binding Path=SelectedSubCategory, Mode=TwoWay}" IsEditable="False" TextBoxBase.TextChanged="SubCategory_ComboBox_TextChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="Aqua" Width="16" Height="16" Margin="0,2,5,2" ></Rectangle>
<TextBlock Text="{Binding Path=name, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
//值转换器
public class ArgbConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var a = System.Convert.ToByte(values[0]);
var r = System.Convert.ToByte(values[1]);
var g = System.Convert.ToByte(values[2]);
var b = System.Convert.ToByte(values[3]);
return new Color() { A = a, B = b, R = r, G = g };
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
// VIEWMODEL
public ObservableCollection<SubCategory> SubCategories { get; set; } = new ObservableCollection<SubCategory>() { new SubCategory() { name = "person" } };
// SubCategory类
public class SubCategory
{
//default values - each of these objects will have different argb values
public int A { get; set; } = 95;
public int R { get; set; } = 225;
public int G { get; set; } = 80;
public int B { get; set; } = 25;
public string name { get; set; }
}
答案 0 :(得分:1)
您已经实现了IMultiValueConverter。因此,显而易见的答案似乎是MultiBinding
,它具有对A,R,G和B属性的四个绑定:
<Rectangle>
<Rectangle.Fill>
<SolidColorBrush>
<SolidColorBrush.Color>
<MultiBinding Converter="{StaticResource argbConverter}">
<Binding Path="A"/>
<Binding Path="R"/>
<Binding Path="G"/>
<Binding Path="B"/>
</MultiBinding>
</SolidColorBrush.Color>
</SolidColorBrush>
</Rectangle.Fill>
</Rectangle>
答案 1 :(得分:1)
您为什么不能使用IValueConverter
而不是IMultiValueConverter
?
XAML
<Rectangle Fill="{Binding Path=., Converter={StaticResource argbConverter}}"
Width="16" Height="16"/>
转换器
public class ArgbConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var item = value as SubCategory;
var a = System.Convert.ToByte(item.A);
var r = System.Convert.ToByte(item.R);
var g = System.Convert.ToByte(item.G);
var b = System.Convert.ToByte(item.B);
return new SolidColorBrush(new Color() { A = a, B = b, R = r, G = g });
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}