【发布时间】:2013-11-01 17:37:52
【问题描述】:
在我的程序 (MVVM WPF) 中有很多枚举,我将枚举绑定到视图中的控件。
有很多方法可以做到这一点。
1) 绑定到 ComboBoxEdit(Devexpress Control)。我正在使用 ObjectDataProvider。
然后这个
<dxe:ComboBoxEdit ItemsSource="{Binding Source={StaticResource SomeEnumValues}>
这可以正常工作,但在 TabControl 标头中却不行。
2) 所以,我想用 IValueConverter 也没有用。
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
if (!(value is Model.MyEnum))
{
return null;
}
Model.MyEnum me = (Model.MyEnum)value;
return me.GetHashCode();
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return null;
}
关于 XAML:
<local:DataConverter x:Key="myConverter"/>
<TabControl SelectedIndex="{Binding Path=SelectedFeeType,
Converter={StaticResource myConverter}}"/>
3) 第三种方法是做一个行为依赖属性
像这样的
public class ComboBoxEnumerationExtension : ComboBox
{
public static readonly DependencyProperty SelectedEnumerationProperty =
DependencyProperty.Register("SelectedEnumeration", typeof(object),
typeof(ComboBoxEnumerationExtension));
public object SelectedEnumeration
{
get { return (object)GetValue(SelectedEnumerationProperty); }
set { SetValue(SelectedEnumerationProperty, value); }
}
我想知道处理枚举和绑定到它的最佳方式是什么。现在我无法将 tabheader 绑定到枚举。
【问题讨论】:
-
目标到底是什么?只需将选项卡标题标签绑定到枚举值?
-
是的,还有一种常用的方法来获取可用于绑定到任何控件的枚举值。比如使用 ObjectDataProvied 或使用 Converters。
-
好吧,我认为#2 应该可以工作——但您不需要双向绑定并实现“ConvertBack”方法吗?
-
你能分享你的 TabControl 的 XAML 吗?