Click here to Skip to main content
15,887,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<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>

I do not know where should I put the target property.
Can someone help me?

Igal
Posted
Updated 3-Jul-10 10:34am
v2

1 solution

In my opinion, the best way to achieve what you need is:

Create an class like:
C#
public class Item
{
    public object Tag{get;set;}
    public object Value{get;set;}
}


then in your ViewModel, you add a collection of Items and a Property to get the selected item of the Item Collection like:
C#
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>


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:

XML
<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>


Hope this helps.
Best regards,
Raul Mainardi Neto
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900