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

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

问题描述

在我的WPF应用程序中,我有一个ComboBox,它包含一个ComboBoxItem的静态列表,因为它的内容永远不会改变。但是,因为我想将SelectedItem数据绑定到我的底层ViewModel,我想让每个ComboBoxItem也有一个单独的值被分配给我的ViewModel属性。我有一些麻烦让这个工作。



我的ComboBox声明如下:

 < ComboBox Height =23Horizo​​ntalAlignment =StretchMargin =2Name =comboBox1VerticalAlignment =Top
SelectedItem ={Binding Path = Mode = TwoWaySelectedValuePath =Tag>
< ComboBoxItem Content =NoneTag =0/>
< ComboBoxItem Content =FewTag =1/>
< ComboBoxItem Content =SomeTag =2/>
< ComboBoxItem Content =EnoughTag =3/>
< ComboBoxItem Content =LotsTag =4/>
< ComboBoxItem Content =Too muchTag =5/>
< / ComboBox>

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

  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的所选项目时,调试跟踪告诉我:

  System.Windows.Data错误:23:无法将System.Windows.Controls.ComboBoxItem:Some从键入'ComboBoxItem'为'en-US'文化键入'System.Int32'
System.Windows.Data错误:7:ConvertBack不能转换值'System.Windows.Controls.ComboBoxItem:Some'(键入'ComboBoxItem')。 (...)System.NotSupportedException:Int32Converter无法从System.Windows.Controls.ComboBoxItem转换。

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

解决方案

如果您使用 SelectedValuePath 然后您需要绑定到 SelectedValue 属性


获取或设置通过使用SelectedValuePath获取的SelectedItem的值


所以修改绑定到

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

因为你经历了 SelectedItem 将始终包含实际选择的项目(在您的情况下, ComboBoxItem )不是该值。


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.

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>

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

    //...
}

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.

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

解决方案

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

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

So modify your binding to

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

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天全站免登陆