Click here to Skip to main content
15,936,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am stuck. I have a WPF page with a listbox that has binding values from codebehind(.cs) file. The text displayed is Active, Pending, Discharged, etc. When I select Active, I need to get string "A". How do I do this?

In the XAML

XML
DisplayMemberPath="ContentName" ItemsSource="{Binding}" SelectedValuePath="ContentCode">


In the CS

C#
public PatientSearch()
{
    InitializeComponent();

    var data = new[] {
       new ListBoxData {ContentCode="A", ContentName="Active"} ,
       new ListBoxData {ContentCode="P", ContentName="Pending"} ,
       new ListBoxData {ContentCode="D", ContentName="Discharge"} ,
       new ListBoxData {ContentCode="I", ContentName="Interrupted"},
       new ListBoxData {ContentCode = "X", ContentName = "Archived"}
    };
    // Bind Array List with the ListBox
    LbPatStat.ItemsSource = data;
}

public class ListBoxData
{
    public string ContentCode { get; set; }
    public string ContentName { get; set; }
}


What I have tried:

I have tried to use IF/Else ans Switch statements, but each of them pass Active and not A...
Posted
Updated 28-Mar-16 20:39pm
v2

1 solution

Hi, just do the following in ListBox SelectionChanged event,
C#
private void LbPatStat_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   ListBoxData SelectedListBoxData = (ListBoxData)LbPatStat.SelectedItem;
   MessageBox.Show("SelectedListBoxData : " + SelectedListBoxData.ContentCode);
}
 
Share this answer
 
Comments
Derek Kennard 4-Apr-16 0:18am    
A few minor tweaks, but the concept was exactly what I needed. Thank you

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