如何将argb值绑定到组合框项目中的矩形填充属性



我有一个自定义对象的组合,每个对象都显示为彩色矩形,并在其旁边显示为文本。

我有要从对象显示的ARGB值,但是我想将其转换为颜色并将其设置为矩形的填充属性。到目前为止,我有以下内容,根据这篇文章WPF

中颜色的b g b b b b b b b b b b b b b b b

您如何指定子类别中的属性以传递到值转换器?以及如何将转换器的结果设置为矩形的填充属性?

//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" } };

//子类别类

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; }
}

为什么不能使用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();
    }
}

您已经实现了一个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>

最新更新