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?

原文链接:http://stackoverflow.com/questions/562682/how-can-i-list-colors-in-wpf-with-xaml

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" />

Here is a great ItemTemplate to use for a combobox using casperOne's code:

<ComboBox Name="cboColors"
ItemsSource="{Binding Source={StaticResource colorPropertiesOdp}}"
SelectedValuePath="Name">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="18" Margin="0,0,0,2">
<Border BorderThickness="1" CornerRadius="2"
BorderBrush="Black" Width="50" VerticalAlignment="Stretch"
Background="{Binding Name}"/>
<TextBlock Text="{Binding Name}" Margin="8,0,0,0"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

Here is what I have done in a past ASP.net app:

// populate colors drop down (will work with other kinds of list controls)
Type colors = typeof(System.Drawing.Color);
PropertyInfo[] colorInfo = colors.GetProperties(BindingFlags.Public |
BindingFlags.Static);
foreach ( PropertyInfo info in colorInfo)
{
ddlColor.Items.Add(info.Name);
} // Get the selected color
System.Drawing.Color selectedColor =
System.Drawing.Color.FromName(ddlColor.SelectedValue);

Here's how to do it in code using reflection. The following will dump all predefined WPF color names to Output:

using System.Reflection;

void ListAllColors()
{
Type colorsType = typeof(System.Windows.Media.Colors);
PropertyInfo[] colorsTypePropertyInfos = colorsType.GetProperties(BindingFlags.Public | BindingFlags.Static); foreach (PropertyInfo colorsTypePropertyInfo in colorsTypePropertyInfos)
Debug.WriteLine(colorsTypePropertyInfo.Name);
}

And to put them in a combobox, you could simply change the last line to:

_comboBox.Items.Add(colorsTypePropertyInfo.Name); 
 

How can I list colors in WPF with XAML?的更多相关文章

  1. WPF:XAML概述

    简介 XAML是eXtensible Application Markup Language可扩展应用程序标记语言,它是微软公司为构建应用程序用户界面而创建的一种新的描述性语言.XAML提供了一种便于 ...

  2. wpf中xaml的类型转换器与标记扩展

    原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...

  3. 【C#】WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的

    原文:[C#]WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的 初学WPF,知道一些控件可以通过定义Style的Trigger改变要显示的样式,但是 ...

  4. 整理:WPF中Xaml中绑定枚举的写法

    原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...

  5. .Net Core WPF之XAML概述

    原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...

  6. Wpf读写Xaml文件

    前言 本文主要介绍Wpf读写Xaml文件. 读写实现 首先我们使用XamlWriter将Wpf的对象转换为Xaml字符串,代码如下: var btn = sender as Button; strin ...

  7. WPF整理-XAML构建后台类对象

    1.XAML 接触WPF的第一眼就是XAML---XAML是用来描绘界面的.其实不然! "Actually, XAML has nothing to do with UI. It's mer ...

  8. Wpf之Xaml属性值和特性值(一)

    其实我一直很好奇在xaml中,通过Attribute=Value这种方式可以进行对元素的描述, 例如: <Rectangle Name=” rectangle” Fill=”Blue”/> ...

  9. WPF入门:XAML

    XAML是WPF技术中专门用于设计UI的语言 XAML优点最大的优点是将UI与逻辑代码剥离 创建第一个WPF应用程序 VS默认生成的WPF项目解决方案 Properties:里面主要包含了程序用到的一 ...

随机推荐

  1. ASP.NET MVC 缓存Outputcache (局部动态)

    首先说一下需求: 比如我需要对网站首页做缓存,于是在首页对于的Action上贴上了Outputcache,接着问题就来了,首页上的有部分数据是不能做缓存的,比如个人信息,不然,每个人登陆都是看到第一个 ...

  2. CF 986A Fair——多源bfs

    题目:http://codeforces.com/contest/986/problem/A 如果从每个村庄开始bfs找货物,会超时. 发现k较小.那就从货物开始bfs,给村庄赋上dis[ 该货物 ] ...

  3. 把Azure专线从Class模式迁移到ARM模式

    前面几篇文章介绍了Azure的ASM模式和ARM模式.很多用户已经在ASM模式下部署了Azure的专线服务,如果部署的应用是ARM模式,或ASM模式和ARM模式都有,就需要把ASM模式的专线迁移到AR ...

  4. Day3-Python基础3--局部变量和全局变量

    一.局部变量 def test(name): print("before change:",name) name = "maqing" #局部变量name,只能 ...

  5. Java-API:java.util百科

    ylbtech-Java-API:java.util百科 包含集合框架.遗留的 collection 类.事件模型.日期和时间设施.国际化和各种实用工具类(字符串标记生成器.随机数生成器和位数组.日期 ...

  6. DCloud-wap2app:杂项

    ylbtech-DCloud-wap2app:杂项 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   ...

  7. 深入VR之前 你应该知道VR头显透镜原理

    转自:http://www.gamelook.com.cn/2016/03/246817 要理解虚拟现实头显透镜的工作原理,首先要搞懂眼睛是如何看到事物的. 眼睛瞳孔后有晶状体,也就是眼珠子.眼睛的背 ...

  8. 利用DNS进行传输数据

    曾经有这样一道题目,困了我数个小时,最后我尝试利用此方法时我知道真相的时候,眼泪已掉下来. 遇到的是一个流量分析题,分析DNS数据,拿到flag 流量如图所示: 进入linux,提取: [root@s ...

  9. HDLM命令dlnkmgr详解之二__help/clear

    1.help操作 主要显示命令的帮助信息. 显示所有操作的帮助信息 -bash-3.2# dlnkmgr help dlnkmgr: Format dlnkmgr { clear | help | o ...

  10. HDLM命令dlnkmgr详解之一__命令格式

    dlnkmgr命令格式 dlnkmgr operation [parameter [parameter-value]] dlnkmgr - The command name. operation - ...