简体   繁体   English

在WPF中将字典绑定到组合框显示不正确

[英]Binding a Dictionary to a Combobox in WPF displays incorrectly

Resolved - The theme was interfering with the display 已解决-主题干扰了显示

This is my first experience with WPF so there may be an obvious answer to this. 这是我第一次使用WPF,因此可能会有一个明显的答案。

I'm trying to display a Month selection combobox where the month names are displayed, and when a selection is made the integer value is captured. 我试图显示一个显示月份名称的月份选择组合框,并在进行选择时捕获整数值。

XAML XAML

<ComboBox Margin="5" IsEditable="False" 
         IsEnabled="{Binding IsCompanyFileUploadPeriodEnabled}" 
         ItemsSource="{Binding StartMonths}" 
         DisplayMemberPath="Key" 
         SelectedValuePath="Value" 
         SelectedValue="{Binding SelectedStartMonthID}" 
         Width="50"></ComboBox>

Edit: The ViewModel extends the Galasoft MvvmLight ViewModelBase, which provides the RaisePropertyChanged method. 编辑: ViewModel扩展了Galasoft MvvmLight ViewModelBase,它提供了RaisePropertyChanged方法。

ViewModel 视图模型

Dictionary<string, int> _startMonths;
public Dictionary<string, int> StartMonths
{
    get
    {
        if (_startMonths == null)
        {
            _startMonths = new Dictionary<string, int>();
            for (int i = 1; i < 13; i++)
            {
                _startMonths.Add(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i),
                    i);
            }
        }
        return _startMonths;
    }
}

int _selectedStartMonthID;
public int SelectedStartMonthID
{
    get
    {
        return _selectedStartMonthID;
    }
    set
    {
        _selectedStartMonthID = value;
        RaisePropertyChanged(() => SelectedStartMonthID);
    }
}

But for some reason when I run the app the combobox is displaying as 但是由于某种原因,当我运行应用程序时,组合框显示为

  • [January, 1] [1月1日]
  • [February, 2] [2月2日]
  • etc 等等

Does anyone know why it might be ignoring the DisplayMemberPath instruction? 有谁知道为什么它可能会忽略DisplayMemberPath指令? The SelectedValuePath setting seems to be working fine when an element is selected. 选择一个元素时,SelectedValuePath设置似乎工作正常。

ComboBox DisplayMemberPath binding is broken by Themes BureauBlue and WhistlerBlue ComboBox DisplayMemberPath绑定被主题局Blue和WhistlerBlue破坏了

http://wpf.codeplex.com/workitem/10129 http://wpf.codeplex.com/workitem/10129

Take a look at StringFormat. 看一下StringFormat。

MSDN StringFormat MSDN StringFormat

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM