惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
酷 壳 – CoolShell
酷 壳 – CoolShell
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 司徒正美
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler
I
InfoQ
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
The Cloudflare Blog
罗磊的独立博客

博客园 - 骑牛射雕

Visual Studio 安装 旧版本(.NET Framework 4.0 和 4.5) 安装程序提示SWbemObjectSet:无效类处理方案 VSTO开发入门,CustomUI元素详解 线程池ThreadPool c# 相同类 赋值 WPF 使用Path绘制几何图形 Prism简介 WPF使用PATH来画圆 经过指定的时间后自动关闭的模式窗体 非对称加密 inno setup 安装前判断进程是否存在,以及停止相应进程 Inno Setup打包CreateProcess 740出错 C#委托与事件应用场景 X.509数字证书的结构与解析 C#action和func的使用 Oracle 分析函数(10G)语法详解 纯JS写的无刷新实时同步购物车系统 图片延时加载插件(lazyload) 几种调用WebService的方法
wpf combobox 绑定枚举
骑牛射雕 · 2023-06-14 · via 博客园 - 骑牛射雕

1、先定义个枚举转换类
/// <summary>
/// 枚举转换
/// </summary>
public class EnumerationExtension : MarkupExtension
{
private Type _enumType;

/// <summary>
/// 枚举转换
/// </summary>
/// <param name="enumType">zhi</param>
public EnumerationExtension(Type enumType)
{
if (enumType == null)
throw new ArgumentNullException("enumType");

EnumType = enumType;
}
/// <summary>
/// enum
/// </summary>
public Type EnumType
{
get { return _enumType; }
private set
{
if (_enumType == value)
return;

var enumType = Nullable.GetUnderlyingType(value) ?? value;

if (enumType.IsEnum == false)
throw new ArgumentException("Type must be an Enum.");

_enumType = value;
}
}
/// <summary>
/// ProvideValue
/// </summary>
/// <param name="serviceProvider">1</param>
/// <returns>object</returns>
public override object ProvideValue(IServiceProvider serviceProvider)
{
var enumValues = Enum.GetValues(EnumType);

return (
from object enumValue in enumValues
select new EnumerationMember
{
Value = enumValue,
Description = GetDescription(enumValue)
}).ToArray();
}

private string GetDescription(object enumValue)
{
var descriptionAttribute = EnumType
.GetField(enumValue.ToString())
.GetCustomAttributes(typeof(DescriptionAttribute), false)
.FirstOrDefault() as DescriptionAttribute;


return descriptionAttribute != null
? descriptionAttribute.Description
: enumValue.ToString();
}
/// <summary>
/// EnumerationMember
/// </summary>
public class EnumerationMember
{
/// <summary>
/// Description
/// </summary>
public string Description { get; set; }
/// <summary>
/// Value
/// </summary>
public object Value { get; set; }
}
}

2、在wpf中使用

先引用上边的转换类文件
xmlns:comModel1="clr-namespace:NE.Model.Extend;assembly=NE.Model.Extend"

方法一:直接用
<ComboBox Grid.Row="2" Grid.Column="1" ItemsSource="{Binding Source={util:Enumeration {x:Type comModel:EGetWorkStepType}}}"
DisplayMemberPath="Description" SelectedValue="{Binding DataFilte_WorkTypeModel}" SelectedValuePath="Value"/>


方法二:DataGrid中使用
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn Header="方法" Width="10*" MinWidth="55" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
Width="{Binding ElementName=Method, Path=ActualWidth}"
Margin="-10,0,0,0"
Padding="10,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding Source={util:Enumeration {x:Type comModel1:ERequestMethod}}}" DisplayMemberPath="Description"
SelectedValue="{Binding Method, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Value">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>