WPF ComboBox:静态列表ComboBoxItems,但是数据绑定SelectedItem? [英] WPF ComboBox: static list of ComboBoxItems, but databound SelectedItem?

查看:3894
本文介绍了WPF ComboBox:静态列表ComboBoxItems,但是数据绑定SelectedItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,我有一个ComboBox填充一个静态列表的ComboBoxItems,因为它的内容永远不会改变。但是,因为我想将SelectedItem数据绑定到我的基础ViewModel,我想要每个ComboBoxItem也有一个单独的值被分配给我的ViewModel属性。

In my WPF application, I have a ComboBox that is filled with a static list of ComboBoxItems because its contents will never change. However, because I want to databind the SelectedItem to my underlying ViewModel, I want each ComboBoxItem to also have a separate value that is to be assigned to my ViewModel property. And I'm having a bit of trouble to get this working.

我的ComboBox声明如下:

My ComboBox declaration looks like:

    <ComboBox Height="23" HorizontalAlignment="Stretch" Margin="2" Name="comboBox1" VerticalAlignment="Top"
              SelectedItem="{Binding Path=Amount, Mode=TwoWay}" SelectedValuePath="Tag" >
        <ComboBoxItem Content="None" Tag="0" />
        <ComboBoxItem Content="Few" Tag="1" />
        <ComboBoxItem Content="Some" Tag="2" />
        <ComboBoxItem Content="Enough" Tag="3" />
        <ComboBoxItem Content="Lots" Tag="4" />
        <ComboBoxItem Content="Too much" Tag="5" />
    </ComboBox>

此ComboBox的SelectedItem绑定到ViewModel的Amount属性,该属性声明为一个整数: / p>

The SelectedItem of this ComboBox is bound to the ViewModel's Amount property, which is declared as an integer:

public class MyViewModel : INotifyPropertyChanged
{
    private int _amount = 3;

    public int Amount
    {
        get { return _amount; }
        set
        {
            _amount = value;
            OnPropertyChanged("Amount");
        }
    }

    //...
}

我希望SelectedValuePath =Tag告诉WPF它应该使用Tag值绑定到ViewModel的Amount属性,但是当我运行这个应用程序并更改ComboBox的选择项时,调试跟踪告诉我:

I was hoping that SelectedValuePath="Tag" would tell WPF that it should use the Tag value to bind to the ViewModel's Amount property, but when I run this application and change the ComboBox's selected item, the debug trace tells me:

System.Windows.Data Error: 23 : Cannot convert 'System.Windows.Controls.ComboBoxItem: Some' from type 'ComboBoxItem' to type 'System.Int32' for 'en-US' culture ...
System.Windows.Data Error: 7 : ConvertBack cannot convert value 'System.Windows.Controls.ComboBoxItem: Some' (type 'ComboBoxItem'). (...) System.NotSupportedException: Int32Converter cannot convert from System.Windows.Controls.ComboBoxItem.

显然,它试图将整个ComboBoxItem绑定到我的ViewModel,而不仅仅是它的Tag值。
我做错了什么?

Apparently, it tries to bind the entire ComboBoxItem to my ViewModel, not just its Tag value. What am I doing wrong?

推荐答案

如果使用 SelectedValuePath 那么您需要绑定到 SelectedValue 属性

If you use SelectedValuePath then you need to bind to the SelectedValue property which is


获取或设置通过使用SelectedValuePath

Gets or sets the value of the SelectedItem, obtained by using SelectedValuePath

因此将您的绑定修改为

SelectedValue="{Binding Path=Amount, Mode=TwoWay}" 

,因为您遇到了 SelectedItem 将总是包含实际选择的项目(在您的情况下 ComboBoxItem )而不是值。

because as you experienced the SelectedItem will always contain the actually selected item (in your case the ComboBoxItem) not the value.

这篇关于WPF ComboBox:静态列表ComboBoxItems,但是数据绑定SelectedItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆