簡體   English   中英

WPF ComboBox as System.Windows.Media.Colors

[英]WPF ComboBox as System.Windows.Media.Colors

想要在我的WPF ListView列中獲得顏色組合框(參見圖像)行為。 照片

有人可以幫助我開始嗎? 我對ListView綁定很滿意,但不確定如何實現它。

編輯:

 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>

告訴我提供的類型必須是枚舉。

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

帶有ItemTemplate的ComboBox

你將不得不為你的ComboBox項目使用ItemTemplate:

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

刷轉換器

此外,您將需要一種顏色來刷轉換器,因為綁定不會自動執行:

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

顏色名稱 - 顏色對創建

以下是如何創建顏色名稱 - 顏色對 (目前它是主窗口類中的實例方法,但您可以將其重構為某個輔助類):

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

窗口代碼

這是窗口:

/// <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的

一些代碼文件:

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對象數據提供者:

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

XAML組合框:

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

應該是這樣的:

 <ComboBox ItemsSource="{Binding ItemSourceObs}">     
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Rectangle Fill="{Binding Color}" Height="10"  Width="10" />
                        <TextBlock Text="{Binding DisplayedText}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>    
        </ComboBox>

其中DisplayesText和Color(Brushes類型)是對象的屬性,可以說A和ItemSourceObs是A類型的ObservableCollection

此方法基於MVVM模式

使用工作解決方案背后的代

 <ComboBox x:Name="ComboColor" Width="50" Height="50"  >     
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>    
    </ComboBox>

和代碼背后:

public MainWindow()
    {
        InitializeComponent();
        ComboColor.ItemsSource = typeof(Colors).GetProperties();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM