Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi there,

I have a wpf application which has combo box and multiple text boxes.

On the window load event the combo box is filled with the Employee IDs. When I select any ID then the below text boxes should display the name, city, zip, country of the selected "Id".
I did wrote the below code for filling in the values in text boxes.
VB
Private Sub ViewMode(ByVal GUID As String)
        Try

            Dim viewDs As New DataSet()
            Dim Query As String
            Query = "Select * from tblSystemEmployeeInfo Where ID = '" & GUID.Trim & "'"
            viewDs = GetResultDataset(Query, True)

            Dim dv As DataView = New DataView(viewDs.Tables(0))
            Dim Notify As String = String.Empty
            If dv.Count > 0 Then

                If Not IsDBNull(dv(0)("ID")) Then
                    txtHWGUID.Text = dv(0)("ID")
                Else
                    txtHWGUID.Text = String.Empty
                End If

               

                If Not IsDBNull(dv(0)("StreetName"))     Then
                    txtHWStName.Text = dv(0)("StreetName")
                Else
                    txtHWStName.Text = String.Empty
                End If
            End If
        Catch ex As Exception
            MsgBox("No Values Found")
        End Try
    End Sub

And called the above function on Selection Changed Event of Combo Box as below:
VB
Private Sub ID CB_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles IDCB.SelectionChanged
        StreetCB.IsEnabled = False
ViewMode(IDCB.SelectedItem)


Though it hits the ViewMode Function but it never populates any values in my text boxes.

Am I missing any thing in it?
Is there any way I could achieve?

Any help will be much appreciated.

Thank you so much!
Posted
Updated 22-May-12 18:26pm
v2

Hi,

in WPF if you don't use the binding infrastructure then you are missing a lot.

In the following sample I tried to mimic your case, and use the binding to do exactly that,

the only drawback is that the customers data are present on the client, they are not fetched from the server on demand

1. make sure to create a class that represents your model,
C#
public class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
   public int Age { get; set; }
}

2. pack your class instances in a list (in my sample an IEnumerable of customers)
3. give that collection to the items source property of the combobox at the load event,
C#
Loaded += (sender, args) => {
   var customers = from i in Enumerable.Range(1, 10)
                   select
                       new Customer() {
                         Id = i,
                         Name = "Customer " + i.ToString(),
                         Age = i + 20};
   comboBox1.ItemsSource = customers;
};

by this you have a combobox that is populated by that list, each combobox item will print the type name you created --> the default behavior is to call ToString, you have 2 solutions here,
first --> go back to your custom class and override ToString to just return the Id,
second --> make use of the DisplayMemberPath of the combobox by giving it the property to display
XML
<combobox height="23" displaymemberpath="Id" horizontalalignment="Left" .../>


now we have the data in the UI, we will use the binding to glue everything together.
XML
<textbox height="23" text="{Binding ElementName=comboBox1, Path=SelectedItem.Name}" .../>
<textbox height="23" text="{Binding ElementName=comboBox1, Path=SelectedItem.Age}" .../>

Now whenever the combo box selected item change, both text boxes will be updated.
hope this solution is helpful to you.
 
Share this answer
 
v2
Actually WPF uses DataBinding instead of setting properties directly from code behind. Try that, it will look very complicted when you start, but get used to it!
When the user changes the selection in a combobox, two SelectionChanged events are fired (at least in a Windows Forms application, I am not sure with WPF): the first event when the previous selection was removed and no item is selected (i.e. IDCB.SelectedItem is Nothing!), and the second when the new item was selected. So make sure to react on the "correct" event only (i.e. do nothing when IDCB.SelectedItem is Nothing). And also set a break point somewhere in the ViewMode function and step thru it to find out more details.
 
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