本文介绍了WPF ComboBox作为System.Windows.Media.Colors的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想在我的WPF ListView列中获得颜色组合框(参见图片)的行为.

Wanting to get the color combobox (see image) behavior in my WPF ListView column.

有人可以帮助我开始吗?我对ListView绑定很满意,但不确定如何实现.

Can someone help me get this started? I am comfortable with ListView binding but not sure how to implement this.

 xmlns:System="clr-namespace:System;assembly=mscorlib"

<ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type System:Enum}"
                    x:Key="ColorList">
   <ObjectDataProvider.MethodParameters>
       <x:Type TypeName="Windows.Media.Color" />
   </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

告诉我输入的类型必须是枚举.

Tells me type provided must be an enum.

我找到的最佳答案:如何使用XAML在WPF中列出颜色?

推荐答案

带有ItemTemplate的组合框

您将必须为ComboBox项目使用ItemTemplate:

ComboBox with ItemTemplate

You will have to use ItemTemplate for you ComboBox items:

    <ComboBox ItemsSource="{Binding NamedColors}"
              xmlns:converters="clr-namespace:TestLab.WPF">
        <ComboBox.Resources>
            <converters:ColorToSolidBrushConverter x:Key="ColorToBrush"/>
        </ComboBox.Resources>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Border BorderThickness="0" Height="20" Width="20" 
                            Background="{Binding Value, Converter={StaticResource ColorToBrush}}"/>
                    <TextBlock Text="{Binding Key}"/>
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

画笔转换器

此外,您将需要颜色画笔转换程序,因为绑定不会自动完成该操作:

Brush converter

Also, you will need a color to brush converter, because binding doesn't do it automatically:

public class ColorToSolidBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new SolidColorBrush((Color)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

颜色名称-颜色对创建

这是创建颜色名称-颜色对的方法(当前是主Window类中的一个实例方法,但您可以将其重构为一些帮助器类):

Color name - color pair creation

And here is how you can create the color Name - color pairs (currently it is an instance method in the main Window class, but you can refactor it to some helper class):

    private IEnumerable<KeyValuePair<String, Color>> GetColors()
    {
        return typeof(Colors)
            .GetProperties()
            .Where(prop =>
                typeof(Color).IsAssignableFrom(prop.PropertyType))
            .Select(prop =>
                new KeyValuePair<String, Color>(prop.Name, (Color)prop.GetValue(null)));
    }

窗口代码

这是窗口:

Window code

And this is the window:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.NamedColors = this.GetColors();

        this.DataContext = this;
    }

    public IEnumerable<KeyValuePair<String, Color>> NamedColors
    {
        get;
        private set;
    }
}


ObjectDataProvider

某些代码文件:


ObjectDataProvider

Some code file:

public namespace SomeNamespace 
{
    public static class ColorHelper
    {
        public static IEnumerable<KeyValuePair<String, Color>> GetColors()
        {
            return typeof(Colors)
                .GetProperties()
                .Where(prop =>
                    typeof(Color).IsAssignableFrom(prop.PropertyType))
                .Select(prop =>
                    new KeyValuePair<String, Color>(prop.Name, (Color)prop.GetValue(null)));
        }
    }
}

XAML对象数据提供程序:

XAML object data provider:

...
xmlns:someNamespace="clr-namespace:SomeNamespace"
...
<ObjectDataProvider MethodName="GetColors"
                    ObjectType="{x:Type someNamespace.ColorHelper}"
                    x:Key="ColorList">
</ObjectDataProvider>

XAML comboBox:

XAML comboBox:

<ComboBox ItemsSource="{Binding ColorList}" ...

这篇关于WPF ComboBox作为System.Windows.Media.Colors的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:37