hidden
hidden

Reputation: 3236

WPF selected value , displaymember path inconsistency with listbox and combo box

I have a stored procedure called DropDownIndividuals() which was created using LINQ. The stored procedure returns FullName and Case_Number. I want to set the SelectedValuePath equal to the Case_Number column in my stored procedure. This is how I did it for a listbox and it works.

private void listBox1_Loaded(object sender, RoutedEventArgs e){
    using (ToolboxDataContext toolboxDB = new ToolboxDataContext())//this is linq in action
    {
         var x = toolboxDB.DropDownIndividuals().ToList();//convert to a list
         listBox1.ItemsSource = x;  //bind the data 
         listBox1.SelectedValuePath = "Case_Number";
         listBox1.DisplayMemberPath = "FullName";

         Console.WriteLine(listBox1.SelectedValue.ToString());
         //Result:it shows the case number of the person the user picks.
    }
}

Now I do the same thing for a dropdown combobox AND IT DOES NOT WORK.

private void individualDropDown_Loaded(object sender, RoutedEventArgs e)
{
    using (ToolboxDataContext toolbox = new ToolboxDataContext())
    {
        var individualDropDownBox = toolbox.DropDownIndividuals().ToList();
        individualDropDown.ItemsSource = individualDropDownBox;
        individualDropDown.DisplayMemberPath = "FullName";
        individualDropDown.SelectedValuePath = "Case_Number";
        Console.WriteLine(individualDropDown.SelectedValue.ToString());
    }
}

Why? How can I fix this?

Upvotes: 1

Views: 12739

Answers (4)

Ragesh Punathil
Ragesh Punathil

Reputation: 85

Your class should be public:

public class Place
{
    public string Name { get; set; }
    public string Id { get; set; }
}
foreach (var y in Lists)
{
    listBox1.DisplayMemberPath = "Name";
    listBox1.SelectedValuePath = "Id";
    // Console.WriteLine(y.Case_Number.ToString());
    listBox1.Items.Add(y);
}

Upvotes: 0

Anand B
Anand B

Reputation: 1

Try this:

MetroAreaList metroAreaList = _presenter.GetMetroArea();
foreach (MetroArea metroArea in metroAreaList) {
    lstMetroArea.DisplayMemberPath = "Name";
    lstMetroArea.SelectedValuePath = "ID";
    lstMetroArea.Items.Add(metroArea);
}

It is working....

Upvotes: 0

brunnerh
brunnerh

Reputation: 184441

Why so chaotic? You do not even set properties in the same order, this is equivalent:

<Grid Margin="5">
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <ListBox Grid.Row="0" Grid.Column="0"
             Name="lbData" ItemsSource="{Binding DpData}"
             DisplayMemberPath="Name"
             SelectedValuePath="Id"/>
    <TextBlock Grid.Row="1" Grid.Column="0"
               Text="{Binding ElementName=lbData, Path=SelectedValue}"/>

    <ComboBox Grid.Row="0" Grid.Column="1" VerticalAlignment="Top"
              Name="cbData" ItemsSource="{Binding DpData}"
              DisplayMemberPath="Name"
              SelectedValuePath="Id"/>
    <TextBlock Grid.Row="1" Grid.Column="1"
               Text="{Binding ElementName=cbData, Path=SelectedValue}"/>
</Grid>

screenshot

...and it displays the same ID as expected.

Edit: At startup the selected value of both controls is null by the way.

Upvotes: 4

Rick Sladkey
Rick Sladkey

Reputation: 34240

You are correct, there is an inconsistency of sorts between the way that SelectedValue is treated for ListBox and ComboBox. For ListBox, upon load, if it has the focus, the SelectedValue will correspond to the first item in the data source. For ComboBox even if it has the focus and a data source supplies items, the default SelectedValue will be unset during the Loaded event handler.

This behavior is by design. To make the ComboBox behave like the ListBox set ComboBox.SelectedIndex to "0" where you define the ComboBox in the XAML.

Upvotes: 0

Related Questions