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. 皮肤和DLL和图片等项目文件完全整合到exe中

    C#开发的程序原生界面实在是太丑了,自己又没有美化天赋,所以只能使用皮肤控件了,网上找到了IrisSkin2,包含一个.dll文件和若干ssk后缀的皮肤文件,代码其实很简单的.但是后来发现个问题,就是 ...

  2. bzoj 3527 [Zjoi2014]力——FFT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3527 把 q[ i ] 除掉.设 g[ i ] = i^2 ,有一半的式子就变成卷积了:另一 ...

  3. ASP.NET网站性能提升的几个方法

    1. HTTP 压缩 HTTP 压缩通常用于压缩从服务端返回的页面内容.它压缩HTTP请求和响应,这个会是巨大的性能提升.我的项目是基于Window Server 2003开发的,可以参考这篇文章. ...

  4. java继承如何理解呢??

    总结:我把他弄的无语了.他是诺基亚公司的软件开发师,大学毕业就可以进那么好的公司.实力 package com.bc; //普通类 class yt { public void price() { S ...

  5. Dubbo入门之一:实例1

    原文地址:http://blog.csdn.net/ruishenh/article/details/23180707?utm_source=tuicool 1.   概述 Dubbo是一个分布式服务 ...

  6. 类型:Oracle;问题:oracle 时间加减;结果:ORACLE 日期加减操作

    ORACLE 日期加减操作 无论是DATE还是timestamp都可以进行加减操作. 可以对当前日期加年.月.日.时.分.秒,操作不同的时间类型,有三种方法: 1 使用内置函数numtodsinter ...

  7. c# 代码调用ssis包

    https://docs.microsoft.com/en-us/sql/integration-services/run-manage-packages-programmatically/loadi ...

  8. Android 媒体编解码器(转)

    媒体编解码器 MediaCodec类是用来为低级别的媒体编码和解码的媒体编解码器提供访问.您可以实例化一个MediaCodec类通过调用createEncoderByType()方法来进行对媒体文件进 ...

  9. DAY9-python并发之多进程

    一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...

  10. Yahoo浮沉录

    Yahoo这一路曾经出过很多好技术 然而,任何人如果只是把 Yahoo 当作一家缺乏聚焦的企业来看也许忽视了公司内部的那些创新—偶尔甚至还有一些很好的产品创意.就拿搜索来说吧,我们说的不是付费搜索,而 ...