问题
I am making a homemade text editor and in it trying to make a control used to change the color of the text, on my tool bar I have a combo box in which I want to be able to load system colors into so that the user may change the color of the selected text. I can not figure out how to populate the combo box with these colors, I have tried variation of things I have found on the internet within my page_loaded event, but can not seem to get it to work. Hope you can help
Thanks Beef
回答1:
You can use this code to populate a combo box with list of all colors
Declare a resource
<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>
Then use that resource in combo box like this
<ComboBox Name="comboBox1" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" DisplayMemberPath="Name" SelectedValuePath="Name" />
In order to use sys:string
you will have to include
xmlns:sys="clr-namespace:System;assembly=mscorlib"
来源:https://stackoverflow.com/questions/6834221/load-colors-into-combobox