【问题标题】:How do I bind ARGB values to a rectangle fill property in a combo box item?如何将 ARGB 值绑定到组合框项中的矩形填充属性?
【发布时间】: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


    【解决方案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();
        }
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 2019-09-12
        • 1970-01-01
        • 2017-04-27
        • 2012-08-27
        • 2017-05-25
        • 1970-01-01
        相关资源
        最近更新 更多