WPF ComboBox:ComboBoxItem 的静态列表,但数据绑定 SelectedItem?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12550926/
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
WPF ComboBox: static list of ComboBoxItems, but databound SelectedItem?
提问by Astrotrain
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.
在我的 WPF 应用程序中,我有一个 ComboBox,它填充了 ComboBoxItems 的静态列表,因为它的内容永远不会改变。但是,因为我想将 SelectedItem 数据绑定到我的底层 ViewModel,所以我希望每个 ComboBoxItem 也有一个单独的值,该值将分配给我的 ViewModel 属性。我在让它工作时遇到了一些麻烦。
My ComboBox declaration looks like:
我的 ComboBox 声明如下所示:
<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:
此 ComboBox 的 SelectedItem 绑定到 ViewModel 的 Amount 属性,该属性声明为整数:
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:
我希望 SelectedValuePath="Tag" 会告诉 WPF 它应该使用 Tag 值绑定到 ViewModel 的 Amount 属性,但是当我运行这个应用程序并更改 ComboBox 的选定项时,调试跟踪告诉我:
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?
显然,它试图将整个 ComboBoxItem 绑定到我的 ViewModel,而不仅仅是它的 Tag 值。我究竟做错了什么?
回答by nemesv
If you use SelectedValuePaththen you need to bind to the SelectedValueproperty which is
如果使用,SelectedValuePath则需要绑定到SelectedValue属性
Gets or sets the value of the SelectedItem, obtained by using SelectedValuePath
获取或设置SelectedItem的值,通过SelectedValuePath获取
So modify your binding to
所以修改你的绑定
SelectedValue="{Binding Path=Amount, Mode=TwoWay}"
because as you experienced the SelectedItemwill always contain the actually selected item (in your case the ComboBoxItem) not the value.
因为正如您所经历的那样,SelectedItem将始终包含实际选择的项目(在您的情况下ComboBoxItem)而不是值。

