将颜色加载到组合框中

Bee*_*eef 1 c# xaml combobox text-editor colors

我正在制作一个自制的文本编辑器,并在其中尝试制作一个用于更改文本颜色的控件,在我的工具栏上,我有一个组合框,我希望能够在其中加载系统颜色,以便用户可以更改所选文本的颜色。我不知道如何用这些颜色填充组合框,我尝试了在 page_loaded 事件中在互联网上找到的各种内容,但似乎无法使其工作。希望你能帮忙

谢谢牛肉

Har*_*san 5

您可以使用此代码用所有颜色的列表填充组合框

声明资源

<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>
Run Code Online (Sandbox Code Playgroud)

然后在组合框中使用该资源,如下所示

<ComboBox Name="comboBox1" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" DisplayMemberPath="Name" SelectedValuePath="Name" />
Run Code Online (Sandbox Code Playgroud)

为了使用sys:string你必须包括 xmlns:sys="clr-namespace:System;assembly=mscorlib"