wpf 如何使用 xaml 代码将 ComboBox 绑定到通用字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12990674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:51:52  来源:igfitidea点击:

How to bind a ComboBox to generic dictionary with xaml code

wpfxamlc#-4.0data-binding

提问by MMD MNC

I'm using the following code :

我正在使用以下代码:

private Dictionary<string, string> GetNumber { get; set; }
public ReportsLetterTra()
{
    GetNumber = new Dictionary<string, string>
                            {
                                {"1", "First"},
                                {"2", "Second"}
                            };

    InitializeComponent();
}

xaml code :

xaml 代码:

 <ComboBox 
      DisplayMemberPath="value" 
      SelectedValuePath="key" 
      ItemsSource="{Binding ElementName=reportslettra,Path=GetNumber}"
      SelectedIndex="0" Name="cmbFromNumber" />

Why is not bind GetNumber to cmbFromNumber?!

为什么不将 GetNumber 绑定到 cmbFromNumber?!

Update :

更新 :

my complete code in behind file :

我在隐藏文件中的完整代码:

using System.Collections.Generic;
using System.Windows;

namespace WpfApplication20
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Dictionary<string, string> GetNumber { get; set; }

        public Window1()
        {

            GetNumber = new Dictionary<string, string>
                                    {
                                        {"1", "First"},
                                        {"2", "Second"}
                                    };
            InitializeComponent();

        }
    }
}

my complete xaml code:

我完整的 xaml 代码:

<Window x:Class="WpfApplication20.Window1" Name="eportslettra"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid Height="26">
        <ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" 
               ItemsSource="{Binding ElementName=reportslettra,Path=GetNumber}" 
               SelectedIndex="0"  Name="cmbFromNumber"    />
    </Grid>
</Window>

Where is my wrong? Why is not bind GetNumber to cmbFromNumber?!

我的错在哪里?为什么不将 GetNumber 绑定到 cmbFromNumber?!

回答by devdigital

Your property is marked as private, make it public. Also, correct the casing on your ComboBoxto Valueand Key. Thirdly, your binding expression looks invalid, double check this. Finally, it looks like your properties are part of the view's code behind file. You might consider the MVVM design pattern.

您的财产被标记为private, make it public。此外,更正您的ComboBoxtoValue和上的外壳Key。第三,您的绑定表达式看起来无效,请仔细检查。最后,看起来您的属性是视图代码隐藏文件的一部分。您可能会考虑 MVVM 设计模式。

Update

更新

Your Windowhas the Nameof 'eportslettra', but your binding expression uses the ElementName'reportslettra'. Correct one of them.

WindowName“eportslettra”,但您的绑定表达式使用ElementName“reportslettra”。纠正其中之一。