我正在制作一个自制的文本编辑器,并在其中尝试制作一个用于更改文本颜色的控件,在我的工具栏上,我有一个组合框,我希望能够在其中加载系统颜色,以便用户可以更改所选文本的颜色。我不知道如何用这些颜色填充组合框,我尝试了在我的 page_loaded 事件中在互联网上找到的各种东西,但似乎无法让它工作。希望你能帮忙
谢谢牛肉
最佳答案
您可以使用此代码来填充包含所有颜色列表的组合框
声明一个资源
<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>
然后像这样在组合框中使用该资源
<ComboBox Name="comboBox1" ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}" DisplayMemberPath="Name" SelectedValuePath="Name" />
为了使用 sys:string
你必须包括
xmlns:sys="clr-namespace:System;assembly=mscorlib"
关于c# - 将颜色加载到组合框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6834221/