列出ComboBox中没有透明色的颜色

时间:2012-05-01 18:34:28

标签: wpf

我不会在我的应用程序ComboBox中使用颜色列表,但我不会将此列表包含在透明颜色中。列表中的附加颜色必须来自Colors类。 我在网上搜索并找到类似的东西:

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

<ComboBox Width="80" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}">
                        <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 个答案:

答案 0 :(得分:0)

我建议您从Colors类创建自己的Color个实例列表,并从此列表中排除Colors.Transparent颜色。然后,您可以将此列表绑定到组合框的ItemsSource属性。

要从Color类获取Colors个实例的列表,您可以使用以下代码段:

PropertyInfo[] properties = typeof(Colors).GetProperties();

foreach (PropertyInfo property in properties)
   Color color = property.GetValue(null, null) as Color;