WPF数据绑定:如何使用XAML将枚举绑定到组合框? [英] WPF Data binding: How to data bind an enum to combo box using XAML?

查看:99
本文介绍了WPF数据绑定:如何使用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>

我想做两件事:


  1. 我希望将数据绑定到组合框值的枚举值 DetailsS​​cope 。我不希望直接使用
    绑定枚举值,因为最后一个枚举值将为 OtherDetail 而不是
    其他详细信息(添加一个空格字符和小写字母'd')。

  2. 我希望数据将组合框中选定的值绑定到 AccountDetail
    实例中指定的值目的。你可以帮助我吗?

  1. 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 be OtherDetail instead of Other detail (added a space character and small letter 'd').
  2. I wish to data bind the selected value in the combo box to the one specified in the instance of the AccountDetail object.

谢谢。

更新:我发现这篇文章 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为这个问题。我使用最先进的版本,但你可以使用一个更简单的版本。其他详细信息+正则表达式=其他细节。使返回值更低,然后返回一个字符串与第一个字符UpperCase应该给你预期的结果

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆