将组合框绑定到字典,下拉列表显示键和值

时间:2014-06-01 19:01:44

标签: c# wpf

我一直在绞尽脑汁,但似乎无法解决问题。 我有一个组合框,它在视图模型中显示与字典相关的语言列表,其中包含语言列表和语言键枚举。字典以任何语言显示语言字符串,该键用于设置该语言,一切正常,除了..虽然所选项目显示正确的语言字符串,下拉列表显示键,值对,如: [英语,英语] [法语,法语] 等等 以下是我如何设置组合:

<ComboBox Name="LanguageCombo"
        DisplayMemberPath="Value"
        IsReadOnly="True"
        ItemsSource="{Binding LanguageDictionary}"
        SelectedValue="{Binding Language, Mode=TwoWay}"
        SelectedValuePath="Key">
</ComboBox>

在viewmodel中:

private Dictionary<Languages, string> _languageDictionary = LanguageManager.LanguageDictionary;

public Dictionary<Languages, string> LanguageDictionary
{
    get { return _languageDictionary; }
    set
    {
        if (_languageDictionary != value)
        {
            _languageDictionary = value;
            RaisePropertyChanged("LanguageDictionary");
        }
    }
}

public string Language
{
    get { return LanguageManager.Language.ToString(); }
    set
    {
        if (value != null)
        {
            if (LanguageManager.Language != value)
            {
                // do some stuff
                RaisePropertyChanged("Language");
            }
        }
    }
}

好的,这里还有一些代码,到目前为止,它是一个名为&#39; LanguageManager的类中的属性,用于完成提升,以及一个带有字典的静态类,称为&#39; LanguageNames&#39; ..

LanguageManager:

public static Dictionary<Languages, string> LanguageDictionary
        {
            get { return LanguageNames.CulturedNames[LanguageNames.Culture]; }
        }

LanguageNames:

public static class LanguageNames
{
    public static CultureInfo Culture { get; set; }
    public static readonly Dictionary<CultureInfo, Dictionary<Languages, string>> CulturedNames;

    static LanguageNames()
    {
       CulturedNames = new Dictionary<CultureInfo, Dictionary<Languages, string>>
        {
            {  // english 
                new CultureInfo("en"), new Dictionary<Languages, string>
                {
                    {Languages.English, "English"},
                    {Languages.French, "French"},
                    {Languages.German, "German"},
                    {Languages.Italian, "Italian"},
                    {Languages.Japanese, "Japanese"},
                    {Languages.Portuguese, "Portuguese"},
                    {Languages.Spanish, "Spanish"},
                }
            },
            {   // french
                new CultureInfo("fr"), new Dictionary<Languages, string>
                {
                    {Languages.English, "Anglais"},
                    {Languages.French, "Français"},
                    {Languages.German, "Allemand"},
                    {Languages.Italian, "Italien"},
                    {Languages.Japanese, "Japonais"},
                    {Languages.Portuguese, "Portugais"},
                    {Languages.Spanish, "Espagnol"},
                }
            },

语言枚举:

public enum Languages
{
    English,
    French,
    German,
    Italian,
    Japanese,
    Portuguese,
    Spanish,
}

就像我说的,这一切都很完美,除了下拉列表显示键/值对,而不仅仅是语言字符串..我做错了什么? 我很感激任何帮助或建议。

2 个答案:

答案 0 :(得分:1)

ItemsSource与IEnumerable一起使用以显示项目。字典来源于

IEnumerable<KeyValuePair>

尝试绑定到LanguageDictionary.Values。

另请注意,这些都不是可观察的集合,因此您无法在ComboBox中获取更新。这可能没问题,具体取决于您的情况,但如果您确实需要更新,请查看为您的语言列表公开ObservableCollection。

<强>更新: 我刚尝试使用DisplayMemberPath&amp; SelectedValuePath,它对我来说很好......

<Window x:Class="ComboBoxTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:comboBoxTest="clr-namespace:ComboBoxTest"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <comboBoxTest:MyDictionary x:Key="MyDictionary">
        <system:String x:Key="3">Three</system:String>
        <system:String x:Key="4">Four</system:String>
        <system:String x:Key="5">Five</system:String>
    </comboBoxTest:MyDictionary>
</Window.Resources>
<StackPanel>
    <ComboBox x:Name="ComboBox"
              DisplayMemberPath="Value"
              SelectedValuePath="Key"
              ItemsSource="{StaticResource MyDictionary}">
    </ComboBox>
    <TextBlock Text="{Binding ElementName=ComboBox, Path=SelectedValue}"></TextBlock>
</StackPanel>
</Window>

我有一种有趣的感觉,你的类型引起了一个问题。关键是&#34;语言&#34;并且你绑定到类型&#34;字符串&#34;的值在您的SelectedValuePath中。也许这就是造成你的问题的原因......

答案 1 :(得分:0)

我最终完全走向了另一个方向.. 我在资源字典中放置了正确的语言名称,这些字典也定义了所有应用程序字符串(lang.it.xaml等),然后将它们作为动态资源添加到组合框中

            <ComboBox Name="LanguageCombo"
                      IsReadOnly="True"
                      SelectedIndex="{Binding LanguageIndex, Mode=TwoWay}">
                <ComboBoxItem Content="{DynamicResource LanguageEnglish}"/>
                <ComboBoxItem Content="{DynamicResource LanguageFrench}"/>
                <ComboBoxItem Content="{DynamicResource LanguageGerman}"/>
                <ComboBoxItem Content="{DynamicResource LanguageItalian}"/>
                <ComboBoxItem Content="{DynamicResource LanguageJapanese}"/>
                <ComboBoxItem Content="{DynamicResource LanguagePortuguese}"/>
                <ComboBoxItem Content="{DynamicResource LanguageSpanish}"/>
            </ComboBox>

然后我使用索引从viewmodel属性设置语言:

VM:

    public int LanguageIndex
    {
        get { return _languageIndex; }
        set
        {
            if (value > -1 && _languageIndex != value)
            {
                _languageIndex = value;
                Languages lang = LanguageManager.GetLanguageKey(value);
                LanguageManager.SetLanguage(lang);
                RaisePropertyChanged("LanguageIndex");
            }
        }
    }