作为示例,请使用以下代码:
public enum ExampleEnum { FooBar, BarFoo }
public class ExampleClass : INotifyPropertyChanged
{
private ExampleEnum example;
public ExampleEnum ExampleProperty
{ get { return example; } { /* set and notify */; } }
}
Run Code Online (Sandbox Code Playgroud)
我想要将属性ExampleProperty数据绑定到ComboBox,以便它显示选项"FooBar"和"BarFoo"并在TwoWay模式下工作.最理想的是我希望我的ComboBox定义看起来像这样:
<ComboBox ItemsSource="What goes here?" SelectedItem="{Binding Path=ExampleProperty}" />
Run Code Online (Sandbox Code Playgroud)
目前我在我的Window中安装了ComboBox.SelectionChanged和ExampleClass.PropertyChanged事件的处理程序,我手动执行绑定.
是否有更好或某种规范的方式?您通常会使用转换器吗?如何使用正确的值填充ComboBox?我现在甚至不想开始使用i18n.
编辑
所以回答了一个问题:如何使用正确的值填充ComboBox.
通过静态Enum.GetValues方法中的ObjectDataProvider将Enum值作为字符串列表检索:
<Window.Resources>
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="ExampleEnumValues">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ExampleEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
这个我可以用作我的ComboBox的ItemsSource:
<ComboBox ItemsSource="{Binding Source={StaticResource ExampleEnumValues}}"/>
Run Code Online (Sandbox Code Playgroud) 有谁能解释一下?
替代文字http://www.deviantsart.com/upload/g4knqc.png
using System;
namespace TestEnum2342394834
{
class Program
{
static void Main(string[] args)
{
//with "var"
foreach (var value in Enum.GetValues(typeof(ReportStatus)))
{
Console.WriteLine(value);
}
//with "int"
foreach (int value in Enum.GetValues(typeof(ReportStatus)))
{
Console.WriteLine(value);
}
}
}
public enum ReportStatus
{
Assigned = 1,
Analyzed = 2,
Written = 3,
Reviewed = 4,
Finished = 5
}
}
Run Code Online (Sandbox Code Playgroud) 我有一套枚举
public enum SyncRequestTypeEnum
{
ProjectLevel=1,
DiffSync=2,
FullSync=3
}
Run Code Online (Sandbox Code Playgroud)
我想在下拉列表中显示这些枚举,但ProjectLevel除外.我可以使用linq获取这些详细信息吗?有人可以帮忙吗?
在我的项目中有一个包含组合框的 UI,组合框会列出一些通信协议,例如 TCP/IP、FTP 等
我想使用枚举来呈现通信协议,可能是这样的:
public enum CommuProtocol
{
TCPIP = 0,
FTP,
MPI,
Other
}
Run Code Online (Sandbox Code Playgroud)
那么,如何将枚举值绑定到组合框中的文本。例如,从组合框中选择的文本中,我可以很容易地知道相应的枚举值,反之亦然。我希望将来可以轻松扩展。
文本可能与枚举值等不同,TCP/IP vs TCPIP ...
谢谢!