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

本文关键字:列表显示 键和值 字典 组合 绑定 | 更新日期: 2023-09-27 18:06:05

我一直在绞尽脑汁,但似乎没有解决办法,所以…我有一个组合框,它在视图模型中显示绑定到字典的语言列表,并带有语言列表和语言键枚举。字典以所显示的任何语言显示语言字符串,键用于设置该语言,一切正常,除了..虽然所选项显示的是正确的语言字符串,但下拉列表显示的是键值对,如:(英语、英语)(法语,法语)等。下面是我如何设置组合:

<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");
            }
        }
    }
}

好的,这里有一些代码,到目前为止它是一个属性在一个名为LanguageManager的类中,它做提升,还有一个静态类,字典名为LanguageNames ..

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,
}

就像我说的,这一切都工作完美,除了,下拉列表显示键/值对,而不仅仅是语言字符串。我做错了什么?非常感谢大家的帮助和建议。

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

ItemsSource与IEnumerable一起显示项目。字典派生自

IEnumerable<KeyValuePair>

尝试绑定到LanguageDictionary。值。

另外,注意这两个都不是可观察集合,所以你不会在ComboBox中获得更新。这可能是好的,取决于你的情况,但如果你想要更新,那么看看暴露一个ObservableCollection为你的语言列表。

:我刚刚尝试使用DisplayMemberPath &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>
我有一种奇怪的感觉,你的类型引起了一个问题。键的类型是"Languages",你在你的SelectedValuePath中绑定到类型为"string"的值。

我最终完全走到了另一个方向。我将适当的语言名称放在资源字典中,资源字典还定义了所有应用程序字符串(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>

然后使用索引从视图模型属性设置语言:

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");
            }
        }
    }