C# 如何通过 ObjectDataProvider 将 ComboBox 绑定到通用字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1605845/
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-08-06 19:18:13  来源:igfitidea点击:

How to bind a ComboBox to generic dictionary via ObjectDataProvider

c#wpfxamldata-bindingcombobox

提问by Edward Tanguay

I want to fill a ComboBox with key/value data in code behind, I have this:

我想在后面的代码中用键/值数据填充 ComboBox,我有这个:

XAML:

XAML:

<Window x:Class="TestCombo234.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestCombo234"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="Choices" ObjectType="{x:Type local:CollectionData}" MethodName="GetChoices"/>
    </Window.Resources>
    <StackPanel HorizontalAlignment="Left">
        <ComboBox ItemsSource="{Binding Source={StaticResource Choices}}"/>
    </StackPanel>
</Window>

Code Behind:

背后的代码:

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

namespace TestCombo234
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public static class CollectionData
    {
        public static Dictionary<int, string> GetChoices()
        {
            Dictionary<int, string> choices = new Dictionary<int, string>();
            choices.Add(1, "monthly");
            choices.Add(2, "quarterly");
            choices.Add(3, "biannually");
            choices.Add(4, "yearly");
            return choices;
        }
    }
}

What do I have to change so that the key is the int and the value is the string?

我必须更改什么才能使键是 int 而值是字符串?

采纳答案by Bryan Anderson

To your ComboBox add

到您的 ComboBox 添加

SelectedValuePath="Key" DisplayMemberPath="Value"

回答by Jim

There's an easier way.

有一个更简单的方法。

Convert the enumeration to a Generic.Dictionary object. For example let say you wanted a combo box with the weekday ( just convert the VB to C#)

将枚举转换为 Generic.Dictionary 对象。例如,假设您想要一个带有工作日的组合框(只需将 VB 转换为 C#)

Dim colWeekdays As New Generic.Dictionary(Of FirstDayOfWeek, String)
    For intWeekday As FirstDayOfWeek = vbSunday To vbSaturday
       colWeekdays.Add(intWeekday, WeekdayName(intWeekday))
    Next

RadComboBox_Weekdays.ItemsSource = colWeekdays

In your XAML you only need to set the following to bind to an object:

在您的 XAML 中,您只需要设置以下内容即可绑定到一个对象:

SelectedValue="{Binding Path= StartDayNumberOfWeeek}"  SelectedValuePath="Key" 
DisplayMemberPath="Value" />

The code above can easily be generalized using reflection to handle any enumerations.

上面的代码可以很容易地使用反射来泛化来处理任何枚举。

hope this helps

希望这可以帮助

回答by juagicre

The way DevExpress 17.1.7handles this is setting those properties: DisplayMemberand ValueMember, in case of a dictionary it would be something like this:

DevExpress 17.1.7处理这个的方式是设置这些属性:DisplayMember并且ValueMember,如果是字典,它会是这样的:

DisplayMember="Value" 
ValueMember="Key"