473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Getting combobox selected item

Sl1ver
196 100+
I have a combobox, but I can only get a type of ComboxItem back so I never get the actual value (Tried "SelectedItem" as well)

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

XAML
Expand|Select|Wrap|Line Numbers
  1.  <ComboBox 
  2.             x:Name="cmbAllotment" 
  3.             Grid.Row="1"  
  4.             Margin="5,0,5,0"
  5.             Grid.Column="0"            
  6.             SelectedValue="{Binding Path=AllotmentSelected, Mode=TwoWay}">
  7.                 <ComboBoxItem Content="All (*)" IsSelected="True" />
  8.                 <ComboBoxItem Content="Worcester" />
  9.                 <ComboBoxItem Content="Rawsonville" />            
  10.                 <ComboBoxItem Content="De Doorns" />
  11.                 <ComboBoxItem Content="Touwsrivier" />
  12.         </ComboBox>
  13.  
and here is the code in the ViewModel
Expand|Select|Wrap|Line Numbers
  1.  private string allotmentSelected;
  2.         public string AllotmentSelected
  3.         {
  4.             get { return allotmentSelected; }
  5.             set
  6.             {
  7.                 allotmentSelected = value;
  8.  
  9.                 //case statement to determine the selected allotment
  10.                 if (allotmentSelected != null)
  11.                 {
  12.                     switch (allotmentSelected)
  13.                     {
  14.                         case "Worcester":
  15.                             allotmentCode = "C0850004";
  16.                             break;
  17.                         case "Rawsonville":
  18.                             allotmentCode = "C0850002";
  19.                             break;
  20.                         case "De Doorns":
  21.                             allotmentCode = "C0850001";
  22.                             break;
  23.                         case "Touwsrivier":
  24.                             allotmentCode = "C0850003";
  25.                             break;
  26.                         default:
  27.                             allotmentCode = "*";
  28.                             break;
  29.                     }
  30.  
  31.                 }
  32.  
  33.                 OnNotifyPropertyChanged("AllotmentSelected");
  34.             }
  35.         }
  36.  
May 9 '13 #1
1 9630
Frinavale
9,735 Expert Mod 8TB
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 INotifyPropertyChanged event).

Here is the modified ViewModel I used:
(C#)
Expand|Select|Wrap|Line Numbers
  1. public class TheVM : System.ComponentModel.INotifyPropertyChanged
  2. {
  3.     private string m_allotmentSelected;
  4.     public string AllotmentSelected {
  5.         get { return m_allotmentSelected; }
  6.         set {
  7.             m_allotmentSelected = value;
  8.             if (PropertyChanged != null) {
  9.                 PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("AllotmentSelected"));
  10.             }
  11.         }
  12.     }
  13.  
  14.     public TheVM()
  15.     {
  16.         this.AllotmentSelected = "*";
  17.     }
  18.  
  19.     public event PropertyChangedEventHandler System.ComponentModel.INotifyPropertyChanged.PropertyChanged;
  20.     public delegate void PropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e);
  21. }
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1. Public Class TheVM
  2.     Implements System.ComponentModel.INotifyPropertyChanged
  3.     Private m_allotmentSelected As String
  4.     Public Property AllotmentSelected() As String
  5.         Get
  6.             Return m_allotmentSelected
  7.         End Get
  8.         Set(value As String)
  9.             m_allotmentSelected = value
  10.             RaiseEvent PropertyChanged(Me, New ComponentModel.PropertyChangedEventArgs("AllotmentSelected"))
  11.         End Set
  12.     End Property
  13.  
  14.     Public Sub New()
  15.         Me.AllotmentSelected = "*"
  16.     End Sub
  17.  
  18.     Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
  19. End Class
  20.  
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 SelectedValuePath property and the DisplayMemberPath 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 "AllotmentSelected" property.

So, when you pick an element in the ComboBox, the ViewModel's AllotmentSelected 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:
Expand|Select|Wrap|Line Numbers
  1. <Window x:Class="Window1"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="Window1" Height="500" Width="500"
  5.         xmlns:local="clr-namespace:MyProject">
  6.     <Window.Resources>
  7.         <XmlDataProvider x:Key="AllotmentOptions" XPath="AllotmentOptions/Allotment">
  8.             <x:XData>
  9.                 <AllotmentOptions xmlns="">
  10.                     <Allotment Name="All (*)" Value="*" />
  11.                     <Allotment Name="Worcester" Value="C0850004" />
  12.                     <Allotment Name="Rawsonville" Value="C0850002" />
  13.                     <Allotment Name="De Doorns" Value="C0850001"/>
  14.                     <Allotment Name="Touwsrivier" Value="C0850003" />
  15.                 </AllotmentOptions>
  16.             </x:XData>
  17.         </XmlDataProvider>
  18.         <local:TheVM x:Key="TheVM" />
  19.     </Window.Resources>
  20.     <Grid>
  21.         <Grid.RowDefinitions>
  22.             <RowDefinition Height="Auto" />
  23.             <RowDefinition Height="Auto" />
  24.         </Grid.RowDefinitions>
  25.         <Button x:Name="SwitchAllotment" Click="SwitchAllotment_Click" Content="Switch Allotment" VerticalAlignment="Top"  Margin="5"/>
  26.     <ComboBox x:Name="cmbAllotment" 
  27.               ItemsSource="{Binding Source={StaticResource AllotmentOptions}, XPath=//Allotment}"
  28.               SelectedValue="{Binding Path=AllotmentSelected, Source={StaticResource TheVM}, Mode=TwoWay}"
  29.               SelectedValuePath="@Value"
  30.               DisplayMemberPath="@Name"
  31.               Grid.Row="1" Grid.Column="0" Margin="5,0,5,0" VerticalAlignment="Top">
  32.     </ComboBox>
  33.     </Grid>
  34. </Window>
And here is the button click code that switches the ViewModel's "AllotmentSelected" property to demonstrate that it works properly:

(C#)
Expand|Select|Wrap|Line Numbers
  1. private void SwitchAllotment_Click(System.Object sender, System.Windows.RoutedEventArgs e)
  2. {
  3.     TheVM vm = FindResource("TheVM");
  4.     if (vm.AllotmentSelected == "C0850003") {
  5.         vm.AllotmentSelected = "*";
  6.     } else {
  7.         vm.AllotmentSelected = "C0850003";
  8.     }
  9. }
(VB.NET)
Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub SwitchAllotment_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
  3.     Dim vm As TheVM = FindResource("TheVM")
  4.     If vm.AllotmentSelected = "C0850003" Then
  5.         vm.AllotmentSelected = "*"
  6.     Else
  7.         vm.AllotmentSelected = "C0850003"
  8.     End If
  9. End Sub
-Frinny
Jun 5 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: farseer | last post by:
Hi, I have a combobox who's data source i set to an array of objects (call it MyObject). these objects have get properties: key, value, descr. i set ValueMember to "key", DisplayMember to...
2
by: Alpha | last post by:
How do I change the selected item in a listbox according to a Combox's selected item on the same form? The Combox is from a different table in the same dataset as the Listbox uses. Combox's...
1
by: tupolev | last post by:
I have successfully created a datagrid with a combobox added in a column of the datagrid, I also have written an event that works when you select an other item in the combobox. Know: how can I...
4
by: Michael Turner | last post by:
Hi I am having a problem with a combobox the values are generated from a recordset, I need to beable to change the selected item to the value specified in a configuration file, I have stored the...
2
by: B | last post by:
I'm trying to simply build a form with a combo box containing a list of states. I'd like for there to be NO default selected item, but invariably, the first item in the DataSource is being...
6
by: George | last post by:
Hi all, How can I get the value stored from the selected item and subitems of a listview? Thanks in advance, George
6
by: Smokey Grindle | last post by:
Say I have a combo box with the following simple object Public class MyObject public ID as integer public Name as string public overrides sub ToString() as string return name end sub end...
2
by: Academia | last post by:
I have a combobox that when it drops down the selected item is not highlighted. I'm guessing there is a property that controls this but can't find one. Is there? Should it always be...
2
by: kurtzky | last post by:
i created a form that should function as follows: i will enter a number in a textbox..then it should query from the database all the records which has that number..these records will have a...
1
by: sbandalli | last post by:
Hello, I have a Datagridview which has a combobox,and 2 textbox, The combobox is bound to a Datasource(Database Sql Server and the table name is Category) ,and Datagridview is not bounded to any...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.