作为示例,请使用以下代码:
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) 我有一节课:
public class AccountDetail
{
public DetailScope Scope
{
get { return scope; }
set { scope = value; }
}
public string Value
{
get { return this.value; }
set { this.value = value; }
}
private DetailScope scope;
private string value;
public AccountDetail(DetailScope scope, string value)
{
this.scope = scope;
this.value = value;
}
}
Run Code Online (Sandbox Code Playgroud)
和一个枚举:
public enum DetailScope
{
Private,
Business,
OtherDetail
}
Run Code Online (Sandbox Code Playgroud)
最后,我有一个.xaml文件:
<Window x:Class="Gui.Wpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test"
SizeToContent="WidthAndHeight">
<Grid>
<ComboBox
Name="ScopeComboBox"
Width="120"
Height="23"
Margin="12" /> …Run Code Online (Sandbox Code Playgroud) 我正在开设一个将被其他国家的一些人使用的课程.我必须本地化每条消息,警告ec,以便他们能理解我们的意思.在许多情况下,我实现了我的目标.但是像描述这些属性属性在屁股中是如此痛苦.
这就是我现在所拥有的:
[Category("Editable Values"), Description("Sets the minimum select...")]
public Ampere Simin
{
get
{...}
set
{...}
}
Run Code Online (Sandbox Code Playgroud)
和
[Category("Editable Values"), Description(Localisation.Simin)] // "Localisation" here is the internal resource file that i wrote for messages, warnings, exceptions and -unfortunately- descriptions
public Ampere Simin
{
get
{...}
set
{...}
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要做的.但是不可能以这种方式使用Localisations.关于我可以使用的东西的任何建议而不是它?
可以使用ObjectDataProviderWPF应用程序将枚举的字符串值绑定到ComboBox的ItemsSource,如此问题所示.
但是,在UWP应用程序中使用类似的代码段时,ff.显示错误消息:
"Windows Universal项目不支持ObjectDataProvider."
在UWP中有一个简单的替代方法吗?
我正在开发一个Custom MarkupExtension,我需要一个来自XAML的非字符串参数来构造新对象.是否可以在datacontext范围内的字段上使用非字符串参数绑定?
换句话说,我该怎么办呢?
<ListBox ItemsSource="{Binding Source={local:MyMarkupExtension {x:Type Button},IncludeMethods={Binding Source=CustomerObject.IsProblematic}}}" />
Run Code Online (Sandbox Code Playgroud)
其中IncludeMethods=CustomerObject.IsProblematic给我这个错误:绑定无法在类型"TypeDescriptorExtension"的"IncludeMethods"属性设置.'绑定'只能在DependencyObject的DependencyProperty上设置.
谁能帮我?
谢谢
我有我的enumHelper类包含这些:
public static IList<T> GetValues()
{
IList<T> list = new List<T>();
foreach (object value in Enum.GetValues(typeof(T)))
{
list.Add((T)value);
}
return list;
}
Run Code Online (Sandbox Code Playgroud)
和
public static string Description(Enum value)
{
Attribute DescAttribute = LMIGHelper.GetAttribute(value, typeof(DescriptionAttribute));
if (DescAttribute == null)
return value.ToString();
else
return ((DescriptionAttribute)DescAttribute).Description;
}
Run Code Online (Sandbox Code Playgroud)
我的枚举是这样的:
public enum OutputType
{
File,
[Description("Data Table")]
DataTable
}
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.所有以前的工作都很好.现在我想添加一个新的帮助器来返回BindingList>,所以我可以将任何枚举链接到任何组合使用
BindingList<KeyValuePair<OutputType, string>> list = Enum<OutputType>.GetBindableList();
cbo.datasource=list;
cbo.DisplayMember="Value";
cbo.ValueMember="Key";
Run Code Online (Sandbox Code Playgroud)
为此,我补充说:
public static BindingList<KeyValuePair<T, string>> GetBindingList()
{
BindingList<KeyValuePair<T, string>> list = new BindingList<KeyValuePair<T, string>>();
foreach (T value …Run Code Online (Sandbox Code Playgroud) c# ×4
wpf ×3
combobox ×2
enums ×2
.net ×1
binding ×1
bindinglist ×1
data-binding ×1
generics ×1
localization ×1
properties ×1
uwp ×1
uwp-xaml ×1
xaml ×1