Getting combobox selected item

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    Getting combobox selected item

    I have a combobox, but I can only get a type of ComboxItem back so I never get the actual value (Tried "SelectedIt em" as well)

    How do I get the selected value in string format to do the switch with?

    XAML
    Code:
     <ComboBox 
                x:Name="cmbAllotment" 
                Grid.Row="1"  
                Margin="5,0,5,0"
                Grid.Column="0"            
                SelectedValue="{Binding Path=AllotmentSelected, Mode=TwoWay}">
                    <ComboBoxItem Content="All (*)" IsSelected="True" />
                    <ComboBoxItem Content="Worcester" />
                    <ComboBoxItem Content="Rawsonville" />            
                    <ComboBoxItem Content="De Doorns" />
                    <ComboBoxItem Content="Touwsrivier" />
            </ComboBox>
    and here is the code in the ViewModel
    Code:
     private string allotmentSelected;
            public string AllotmentSelected
            {
                get { return allotmentSelected; }
                set
                {
                    allotmentSelected = value;
    
                    //case statement to determine the selected allotment
                    if (allotmentSelected != null)
                    {
                        switch (allotmentSelected)
                        {
                            case "Worcester":
                                allotmentCode = "C0850004";
                                break;
                            case "Rawsonville":
                                allotmentCode = "C0850002";
                                break;
                            case "De Doorns":
                                allotmentCode = "C0850001";
                                break;
                            case "Touwsrivier":
                                allotmentCode = "C0850003";
                                break;
                            default:
                                allotmentCode = "*";
                                break;
                        }
    
                    }
    
                    OnNotifyPropertyChanged("AllotmentSelected");
                }
            }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    This all depends on what you are binding to.

    For example, if you were to bind to an "allotment" object, you would get the allotment upon switching items.


    I took your example to expand upon this topic.

    I changed the property in your ViewModel so that simply gets and sets a string (and raises an INotifyProperty Changed event).

    Here is the modified ViewModel I used:
    (C#)
    Code:
    public class TheVM : System.ComponentModel.INotifyPropertyChanged
    {
    	private string m_allotmentSelected;
    	public string AllotmentSelected {
    		get { return m_allotmentSelected; }
    		set {
    			m_allotmentSelected = value;
    			if (PropertyChanged != null) {
    				PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("AllotmentSelected"));
    			}
    		}
    	}
    
    	public TheVM()
    	{
    		this.AllotmentSelected = "*";
    	}
    
    	public event PropertyChangedEventHandler System.ComponentModel.INotifyPropertyChanged.PropertyChanged;
    	public delegate void PropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e);
    }
    (VB.NET)
    Code:
    Public Class TheVM
        Implements System.ComponentModel.INotifyPropertyChanged
        Private m_allotmentSelected As String
        Public Property AllotmentSelected() As String
            Get
                Return m_allotmentSelected
            End Get
            Set(value As String)
                m_allotmentSelected = value
                RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("AllotmentSelected"))
            End Set
        End Property
    
        Public Sub New()
            Me.AllotmentSelected = "*"
        End Sub
    
        Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
    End Class
    In my XAML code I define a XmlDataProvider with "Allotment" elements within it. The Allotment elements have 2 attributes Name and Value. I assigned the Name of each allotment element to match what you had in the content of your ComboBoxItems. I then assigned the Value of each allotment element to the values you had in your old VM property.

    Now that I have a ViewModel that works with XML elements which describe an "Allotment" , I can bind the ComboBox to the items and set the ViewModel's property.

    Please note that the ComboBox class has 2 properties that we are particularly interested in: the SelectedValuePa th property and the DisplayMemberPa th property.

    These let us tell the ComboBox which property represents the "Content (to display)" of the ComboBoxItem and which property represents the "Value" to use when the item is selected.

    I have bound the ItemSource of the ComboBox to my Allotment elements and I have indicated that the "Name" attribute of the Allotment element should be used as the display member path and I have indicated that the "Value" attribute of the Allotment element should be used as the selected value path.


    I have also bound the SelectedValue property of the ComboBox to the ViewModel's "AllotmentSelec ted" property.

    So, when you pick an element in the ComboBox, the ViewModel's AllotmentSelect ed property is set to the "Value" attribute associated with the selected the Allotment XML element.


    It is probably best to show you the XAML.
    Here is my XAML code:
    Code:
    <Window x:Class="Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="500" Width="500"
            xmlns:local="clr-namespace:MyProject">
        <Window.Resources>
            <XmlDataProvider x:Key="AllotmentOptions" XPath="AllotmentOptions/Allotment">
                <x:XData>
                    <AllotmentOptions xmlns="">
                        <Allotment Name="All (*)" Value="*" />
                        <Allotment Name="Worcester" Value="C0850004" />
                        <Allotment Name="Rawsonville" Value="C0850002" />
                        <Allotment Name="De Doorns" Value="C0850001"/>
                        <Allotment Name="Touwsrivier" Value="C0850003" />
                    </AllotmentOptions>
                </x:XData>
            </XmlDataProvider>
            <local:TheVM x:Key="TheVM" />
        </Window.Resources>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Button x:Name="SwitchAllotment" Click="SwitchAllotment_Click" Content="Switch Allotment" VerticalAlignment="Top"  Margin="5"/>
        <ComboBox x:Name="cmbAllotment" 
                  ItemsSource="{Binding Source={StaticResource AllotmentOptions}, XPath=//Allotment}"
                  SelectedValue="{Binding Path=AllotmentSelected, Source={StaticResource TheVM}, Mode=TwoWay}"
                  SelectedValuePath="@Value"
                  DisplayMemberPath="@Name"
                  Grid.Row="1" Grid.Column="0" Margin="5,0,5,0" VerticalAlignment="Top">
        </ComboBox>
        </Grid>
    </Window>
    And here is the button click code that switches the ViewModel's "AllotmentSelec ted" property to demonstrate that it works properly:

    (C#)
    Code:
    private void SwitchAllotment_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    {
        TheVM vm = FindResource("TheVM");
        if (vm.AllotmentSelected == "C0850003") {
            vm.AllotmentSelected = "*";
        } else {
            vm.AllotmentSelected = "C0850003";
        }
    }
    (VB.NET)
    Code:
     
    Private Sub SwitchAllotment_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        Dim vm As TheVM = FindResource("TheVM")
        If vm.AllotmentSelected = "C0850003" Then
            vm.AllotmentSelected = "*"
        Else
            vm.AllotmentSelected = "C0850003"
        End If
    End Sub
    -Frinny
    Last edited by Frinavale; Jun 5 '13, 03:25 PM.

    Comment

    Working...