【问题标题】:WPF ListBox DisplayMemberPath and SelectedValuePathWPF ListBox DisplayMemberPath 和 SelectedValuePath
【发布时间】:2011-04-25 21:25:30
【问题描述】:

如何使用 Linq To Sql 将值和键绑定到列表框?

我正在使用 linq to sql 类填充列表框,这是 WPF:

<ListBox  Name="listBox1" Loaded="listBox1_Loaded" />

以下显示FullNames,但不显示Case_Number

using (ToolboxDataContext toolboxDB = new ToolboxDataContext())
{
    var x = toolboxDB.DropDownIndividuals().ToList(); 
    listBox1.ItemsSource = x;
}

我也试过了,还是不行:

foreach (var y in x)
{
    listBox1.DisplayMemberPath = y.FullName.ToString() ;
    listBox1.SelectedValuePath = y.Case_Number.ToString() ;
    // Console.WriteLine(y.Case_Number.ToString());
}

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    在标记中您可以指定要绑定的类型,例如:

    <ListBox DisplayMemberPath="FullName" SelectedValuePath="Case_Number"/>
    

    【讨论】:

    • 不错。现在我做了 private void listBox1_SelectionChanged_1(object sender, SelectionChangedEventArgs e) { Console.WriteLine(listBox1.SelectedValue.ToString()); }工作!!!
    【解决方案2】:

    嗯,

    在 XAML 中:

    <ListBox x:Name="listBox1" 
             DisplayMemberPath="FullName" 
             SelectedValuePath="Case_Number" />
    

    在后面的代码中:

    using (ToolboxDataContext toolboxDB = new ToolboxDataContext())
         PersonsListBox.ItemsSource = toolboxDB.DropDownIndividuals().ToList(); 
    

    【讨论】:

      猜你喜欢
      • 2011-04-17
      • 1970-01-01
      • 2023-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      相关资源
      最近更新 更多