How can I list colors in WPF with XAML?
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?的更多相关文章
- WPF:XAML概述
简介 XAML是eXtensible Application Markup Language可扩展应用程序标记语言,它是微软公司为构建应用程序用户界面而创建的一种新的描述性语言.XAML提供了一种便于 ...
- wpf中xaml的类型转换器与标记扩展
原文:wpf中xaml的类型转换器与标记扩展 这篇来讲wpf控件属性的类型转换器 类型转换器 类型转换器在asp.net控件中已经有使用过了,由于wpf的界面是可以由xaml组成的,所以标签的便利也需 ...
- 【C#】WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的
原文:[C#]WPF的xaml中定义的Trigger为什么有时候会不管用,如Border的MouseOver之类的 初学WPF,知道一些控件可以通过定义Style的Trigger改变要显示的样式,但是 ...
- 整理:WPF中Xaml中绑定枚举的写法
原文:整理:WPF中Xaml中绑定枚举的写法 目的:在Combobox.ListBox中直接绑定枚举对象的方式,比如:直接绑定字体类型.所有颜色等枚举类型非常方便 一.首先用ObjectDataPro ...
- .Net Core WPF之XAML概述
原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...
- Wpf读写Xaml文件
前言 本文主要介绍Wpf读写Xaml文件. 读写实现 首先我们使用XamlWriter将Wpf的对象转换为Xaml字符串,代码如下: var btn = sender as Button; strin ...
- WPF整理-XAML构建后台类对象
1.XAML 接触WPF的第一眼就是XAML---XAML是用来描绘界面的.其实不然! "Actually, XAML has nothing to do with UI. It's mer ...
- Wpf之Xaml属性值和特性值(一)
其实我一直很好奇在xaml中,通过Attribute=Value这种方式可以进行对元素的描述, 例如: <Rectangle Name=” rectangle” Fill=”Blue”/> ...
- WPF入门:XAML
XAML是WPF技术中专门用于设计UI的语言 XAML优点最大的优点是将UI与逻辑代码剥离 创建第一个WPF应用程序 VS默认生成的WPF项目解决方案 Properties:里面主要包含了程序用到的一 ...
随机推荐
- Message类的属性Msg所关联的消息ID
在做C#的Message消息处理的时候,用到了消息的msg编号不知道对应的是什么事件,所以才从网上找来资料如下,在文章最后我会给出资料的出处的. WM_NULL=0x0000 WM_CREATE=0x ...
- ZIP 算法详解 (转!)
zip 的压缩原理与实现(lz77 算法压缩) 无损数据压缩是一件奇妙的事情,想一想,一串任意的数据能够根据一定的规则转换成只有原来 1/2 - 1/5 长度的数据,并且能够按照相应的规则还原到原来的 ...
- es6字符串的扩展学习笔记
1. 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. st ...
- Linux性能监控工具sysstat系列简介
简介 sysstat提供了Linux性能监控的工具集,包括sar.sadf.mpstat.iostat.pidstat等,这些工具可以监控系统性能和使用情况.各工具的作用如下: iostat - 提供 ...
- YUV转换成RGB算法
YUV转换成RGB void yuvtorgb ( double *rgb,unsigned char *yuv) { int i; rgb[] = ] + + ] - ); // r rgb[] = ...
- GWT更改元素样式属性
GWT有时候不像普通网页那样可以自由的添加CSS改变样式,所幸gwt提供了一些底层的方法,通过这些方法来实现DOM操作等.通过gwt部件的getElement()可以取得dom上的元素,这时就能对该元 ...
- 浅析Java中的深拷贝和浅拷
浅析Java中的深拷贝和浅拷贝 原文链接: http://blog.csdn.net/tounaobun/article/details/8491392 假如说你想复制一个简单变量.很简单: in ...
- 高效C#编码优化
1.foreach VS for 语句 Foreach 要比for具有更好的执行效率Foreach的平均花费时间只有for的30%.通过测试结果在for和foreach都可以使用的情况下,我们推荐使用 ...
- spring--AOP--权限---demo1---bai
AOP权限DEMO1: 实体类: package com.etc.entity; import org.aspectj.lang.annotation.Pointcut; public class U ...
- distinct可以用in代替(小技巧)
distinct可以用in代替,in的好处是直接能获取所有数据,而distunct只能获取distinct的字段,不过效率肯定高一些.