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

推荐订阅源

量子位
Vercel News
Vercel News
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
H
Help Net Security
罗磊的独立博客
The Cloudflare Blog
J
Java Code Geeks
博客园 - 叶小钗
I
InfoQ
B
Blog
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
月光博客
月光博客
博客园_首页
雷峰网
雷峰网
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
美团技术团队
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美

博客园 - 骑牛射雕

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>