WPF选中值,显示成员路径与列表框和组合框不一致

时间:2011-04-27 21:57:18

标签: c# wpf

我有一个名为DropDownIndividuals()的存储过程,它是使用LINQ创建的。 存储过程返回FullName和Case_Number。我想将SelectedValuePath设置为等于我的存储过程中的Case_Number列。这就是我为列表框做的事情,它可以工作。

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.
            }
}

现在我为下拉组合框做了同样的事情而且它不起作用。


 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());

             }
}

为什么呢?我该如何解决这个问题?

4 个答案:

答案 0 :(得分:4)

为什么这么混乱?您甚至不以相同的顺序设置属性,是等效的:

<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

...并显示与预期相同的ID。

修改:启动时,两个控件的选定值都是null

答案 1 :(得分:0)

您是对的,SelectedValueListBox处理ComboBox的方式之间存在不一致。对于ListBox,在加载时,如果它具有焦点,则SelectedValue将对应于数据源中的第一项。对于ComboBox,即使它具有焦点且数据源提供项目,也会在SelectedValue事件处理程序中取消设置默认Loaded

此行为是设计使然。要使ComboBox的行为类似于ListBox设置ComboBox.SelectedIndex"0",您在XAML中定义ComboBox

答案 2 :(得分:0)

试试这个:

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

它正在运作......

答案 3 :(得分:0)

你的课程应公开:

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);
}