WPF 数据绑定:如何使用 XAML 将枚举数据绑定到组合框? [英] WPF Data binding: How to data bind an enum to combo box using XAML?
问题描述
我有一堂课:
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;
}
}
和一个枚举:
public enum DetailScope
{
Private,
Business,
OtherDetail
}
最后,我有一个 .xaml 文件:
Lastly, I have a .xaml file:
<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" />
</Grid>
</Window>
我想做两件事:
- 我希望将
DetailsScope
枚举值数据绑定到组合框值.我不想直接绑定枚举值,因为最后一个枚举值将是OtherDetail
而不是其他细节
(添加了一个空格字符和小写字母d"). - 我希望将组合框中选定的值绑定到组合框中指定的值
AccountDetail
对象的实例.
- I wish to data bind
DetailsScope
enum values to the combo box values. I don't wish to bind enum values directly because the last enum value would beOtherDetail
instead ofOther detail
(added a space character and small letter 'd'). - I wish to data bind the selected value in the combo box to the one specified in the
instance of the
AccountDetail
object.
你能帮我一下吗?谢谢.
Could you help me out? Thanks.
更新:我发现这篇文章 http://blogs.msdn.com/b/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx.我需要类似的东西.
Update: I found this post http://blogs.msdn.com/b/wpfsdk/archive/2007/02/22/displaying-enum-values-using-data-binding.aspx. I need something similar.
推荐答案
一个非常简单的方法是使用 ObjectDataProvider
A pretty easy way to do this is to use an ObjectDataProvider
<ObjectDataProvider MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="DetailScopeDataProvider">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:DetailScope" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
使用 ObjectDataProvider 作为 ComboBox 的 ItemsSource,将 SelectedItem 绑定到 Scope 属性并应用转换器来显示每个 ComboBoxItem
Use the ObjectDataProvider as the ItemsSource for the ComboBox, bind SelectedItem to the Scope property and apply a converter for the display of each ComboBoxItem
<ComboBox Name="ScopeComboBox"
ItemsSource="{Binding Source={StaticResource DetailScopeDataProvider}}"
SelectedItem="{Binding Scope}"
Width="120"
Height="23"
Margin="12">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource CamelCaseConverter}}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
在转换器中,您可以将 Regex 用于 这个 问题.我使用了最高级的版本,但您可能可以使用更简单的版本.其他细节 + 正则表达式 = 其他细节.降低返回值然后返回第一个字符为大写的字符串应该会给你预期的结果
And in the converter you can use Regex for CamelCase string splitter found in this question. I used the most advanced version but you can probably use a simplier one. OtherDetail + the regex = Other Detail. Making return value lower and then return a string with first Character UpperCase should give you expected result
public class CamelCaseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string enumString = value.ToString();
string camelCaseString = Regex.Replace(enumString, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ").ToLower();
return char.ToUpper(camelCaseString[0]) + camelCaseString.Substring(1);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
}
这篇关于WPF 数据绑定:如何使用 XAML 将枚举数据绑定到组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!