Load colors into comboBox

强颜欢笑 提交于 2020-01-06 03:25:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!