Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

c# - How can I list colors in WPF with XAML?

How can I get list of all colors I can pick in Visual Studio Designer (which is System.Windows.Media.Colors, but that isn't a collection) and put them into my own ComboBox using WPF and XAML markup?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is the pure XAML solution.

In your resources section, you would use this:

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

Or, as CodeNaked points out, it can be reduced to one tag:

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

And then the combobox would look like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...