【发布时间】:2019-07-19 03:51:53
【问题描述】:
我有一个自定义对象的组合框,每个对象都显示为一个彩色矩形,旁边是一个文本。
我有想要从对象显示的 ARGB 值,但我想将其转换为颜色并将其设置为矩形的填充属性。到目前为止,我有以下基于这篇文章 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
}
//视图模型
public ObservableCollection<SubCategory> SubCategories { get; set; } = new ObservableCollection<SubCategory>() { new SubCategory() { name = "person" } };
//子类别类
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; }
}
【问题讨论】:
标签: wpf