组合框中的多重绑定 [英] Multibinding in Combobox
问题描述
我正在尝试使用MVVM模式实现组合框项目选择.
组合框被xaml中的项目填充",我希望应通过选择项目通知viewmodel.但是...
我想获取商品的名称和商品的标签.
所以我尝试了这样的事情:
Hi,
I am trying to implement a combobox item selection utilizing the MVVM pattern.
The combobox is ''filled'' with items within the xaml and I''d like that the viewmodel shall be notified with an item selection. But...
I''d like to get the name of the item and the tag of the item.
So I tried something like this:
<ComboBox Height="23" Margin="396,487,0,0" Name="PlaybackVideoChannelCombo" VerticalAlignment="Top" Background="YellowGreen" HorizontalAlignment="Left" Width="93">
<ComboBox.SelectedValue>
<MultiBinding Converter="{StaticResource CommandConverter}">
<MultiBinding.Bindings>
<Binding Path="IsChecked" RelativeSource="{RelativeSource Mode=Self}" />
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}" />
</MultiBinding.Bindings>
</MultiBinding>
</ComboBox.SelectedValue>
<ComboBoxItem Background="YellowGreen" Tag="0">Value1</ComboBoxItem>
<ComboBoxItem Background="YellowGreen" Tag="1">Value2</ComboBoxItem>
<ComboBoxItem Background="YellowGreen" Tag="2">Value3</ComboBoxItem>
<ComboBoxItem Background="YellowGreen" Tag="3">Value4</ComboBoxItem>
<ComboBoxItem Background="YellowGreen" Tag="4">Value5</ComboBoxItem>
<ComboBoxItem Background="YellowGreen" Tag="5">Value6</ComboBoxItem>
<ComboBoxItem Background="YellowGreen" Tag="6">Value7</ComboBoxItem>
</ComboBox>
我不知道应该将目标属性放在哪里.
有人可以帮我吗?
Igal
I do not know where should I put the target property.
Can someone help me?
Igal
推荐答案
在我看来,实现所需功能的最佳方法是:
创建一个像这样的类:
In my opinion, the best way to achieve what you need is:
Create an class like:
public class Item
{
public object Tag{get;set;}
public object Value{get;set;}
}
然后在您的ViewModel中,添加Items的集合和Property以获取Item Collection的选定项,例如:
then in your ViewModel, you add a collection of Items and a Property to get the selected item of the Item Collection like:
public ObservableCollection<item> Items{get;set;}
private Item _selectedItem;
public Item SelectedItem
{
get{return _selectedItem;}
set
{
if(_selectedItem != value)
{
_selectedItem = value;
if(PropertyChanged!=null)
PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem");
}
}
}
</item>
然后,通过使用ComboBox的SelectedItemProperty,您将同时获得Value和Tag,但不是在XAML中添加ComboBox的项,而是在ItemsSourceProperty中通过BindingExpression将其添加,例如:
Then by using the SelectedItemProperty of the ComboBox you will get both the Value and Tag, but instead of adding the items of the ComboBox in XAML, you will add it by a BindingExpression in the ItemsSourceProperty, like:
<ComboBox Height="23" Margin="396,487,0,0" Name="PlaybackVideoChannelCombo" VerticalAlignment="Top" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" Background="YellowGreen" HorizontalAlignment="Left" Width="93">
<ComboBox.ItemTemplate>
<TextBlock Text="{Binding Value}"/>
</ComboBox.ItemTemplate>
</ComboBox>
希望这会有所帮助.
最好的问候,
劳尔·迈纳迪(Raul Mainardi Neto)
Hope this helps.
Best regards,
Raul Mainardi Neto
这篇关于组合框中的多重绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!