473,624 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Getting combobox selected item

Sl1ver
196 New Member
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
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 9739
Frinavale
9,735 Recognized Expert Moderator Expert
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#)
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 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:
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 "AllotmentSelec ted" 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
10247
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 "descr", and datasource to the array of MyObjects. How can i set the selected value of that combobox based on the ValueMember OR how can i find the index of an item in the combobox BY ValueMember?
2
3317
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 ValueMember is a ID that is a column in the table that the listbox uses. Is there a way to link these 2's relation so that when the user changes the selection in the Combox the list box would automacticall changes to reflect the items that's linked to...
1
3183
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 set de selected item (as a string) in the textbox of the datagrid ? Tupolev
4
6680
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 value as a varible. I am using the code below but it isn't working any ideas am I using the right method. combobox1.selecteditem = getcolvalue(0) Any help appreciated.
2
2482
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 displayed. It seems to work fine on other forms, but for one particular form, nothing I do seems to work. My code is basically: string SQL = ""; SQL = "SELECT StateAbbreviation FROM States ORDER BY StateAbbreviation";
6
22131
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
5185
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 Class
2
2662
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 highlighted?
2
3677
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 different item no in it..then, these records will be saved in a temporary datatable (which i made in a separate class, the name is WBASKET)...The item nos of these records will be displayed in a combobox, say item1, item2, etc.. then,i have a datagrid...
1
5221
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 datasource. When the user selects an item in the combobox,and enters the item(text) in the 2 text box , I want all the 3(combobox selected item, and the entered text in the 2 textbox) of them to store it a a table name called...
0
8234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8172
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2605
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.