2009-02-18 30 views
14

Jak mogę uzyskać listę wszystkich kolorów, które mogę wybrać w Visual Studio Designer (który jest System.Windows.Media.Colors, ale to nie jest kolekcja) i umieścić je w moim własnym ComboBox przy użyciu znaczników WPF i XAML?Jak mogę wyświetlić kolory w WPF z XAML?

+0

podobne do sugestii CasperOne, oto jest droga zrobić to wszystko w XAML, z ładnym wyświetlaniem wszystkiego w WrapPanel. http://stuff.seans.com/2011/02/14/creating-a-listbox-that-shows-all-predefined-wpf-colors/ –

Odpowiedz

31

Oto czysty roztwór XAML.

W sekcji zasobów, należy użyć tego:

<!-- Make sure this namespace is declared so that it's in scope below --> 
.. xmlns:sys="clr-namespace:System;assembly=mscorlib" .. 

<ObjectDataProvider MethodName="GetType" 
    ObjectType="{x:Type sys:Type}" x:Key="colorsTypeOdp"> 
    <ObjectDataProvider.MethodParameters> 
     <sys:String>System.Windows.Media.Colors, PresentationCore, 
      Version=3.0.0.0, Culture=neutral, 
      PublicKeyToken=31bf3856ad364e35</sys:String> 
    </ObjectDataProvider.MethodParameters> 
</ObjectDataProvider> 
<ObjectDataProvider ObjectInstance="{StaticResource colorsTypeOdp}" 
    MethodName="GetProperties" x:Key="colorPropertiesOdp"> 
</ObjectDataProvider> 

Albo, jak CodeNaked points out, może być zmniejszona do jednego tagu:

<ObjectDataProvider 
    ObjectInstance="{x:Type Colors}" 
    MethodName="GetProperties" 
    x:Key="colorPropertiesOdp" /> 

A potem combobox będzie wyglądać następująco:

<ComboBox Name="comboBox1" 
    ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" 
    DisplayMemberPath="Name" 
    SelectedValuePath="Name" /> 
+4

@casperOne - Twoje rozwiązanie może być skondensowane do jednej linii: '< ObjectDataProvider ObjectInstance = "{x: Wpisz kolory}" MethodName = "GetProperties" x: Key = "colorPropertiesOdp" /> '. Ta odpowiedź jest dość stara, więc może nie działała w tym czasie. Ale testowałem z projektem .NET 3.0, 3.5 i 4.0 i działało to za każdym razem. – CodeNaked

7

Oto co zrobiłem w przeszłości aplikacji ASP.net:

// populate colors drop down (will work with other kinds of list controls) 
Type colors = typeof(System.Drawing.Color); 
PropertyInfo[] colorInfo = colors.GetProperties(BindingFlags.Public | 
    BindingFlags.Static); 
foreach (PropertyInfo info in colorInfo) 
{ 
    ddlColor.Items.Add(info.Name);     
} 

// Get the selected color 
System.Drawing.Color selectedColor = 
    System.Drawing.Color.FromName(ddlColor.SelectedValue); 
10

Oto świetny ItemTemplate do użycia w combobox przy użyciu casperOne's code:

<ComboBox Name="cboColors" 
      ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" 
      SelectedValuePath="Name"> 
    <ComboBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal" Height="18" Margin="0,0,0,2"> 
       <Border BorderThickness="1" CornerRadius="2" 
        BorderBrush="Black" Width="50" VerticalAlignment="Stretch" 
        Background="{Binding Name}"/> 
       <TextBlock Text="{Binding Name}" Margin="8,0,0,0"/> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 
-1

Oto jak to zrobić w kodzie za pomocą odbicia. Poniższa zrzuci wszystkie predefiniowanych nazw kolorów WPF do wyjścia:

using System.Reflection; 

void ListAllColors() 
{ 
    Type colorsType = typeof(System.Windows.Media.Colors); 
    PropertyInfo[] colorsTypePropertyInfos = colorsType.GetProperties(BindingFlags.Public | BindingFlags.Static); 

    foreach (PropertyInfo colorsTypePropertyInfo in colorsTypePropertyInfos) 
     Debug.WriteLine(colorsTypePropertyInfo.Name); 
} 

I umieścić je w combobox, można po prostu zmienić ostatni wiersz do:

_comboBox.Items.Add(colorsTypePropertyInfo.Name);