2012-10-21 47 views
2

我想将CustomerList\CustomerName属性项目显示为ListBox使用ItemsSourceDisplayMemberPath仅属性。但它不起作用。我不想在我的问题中使用DataContext或任何其他绑定。请帮忙。 我的代码如下:DisplayMemberPath在WPF中不工作

MainWindow.xaml.cs

namespace BindingAnItemControlToAList 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
    } 

    public class Customer 
    { 
     public string Name {get;set;} 
     public string LastName { get; set; } 
    } 
    public class CustomerList 
    { 
     public List<Customer> Customers { get; set; } 
     public List<string> CustomerName { get; set; } 
     public List<string> CustomerLastName { get; set; } 
     public CustomerList() 
     { 
      Customers = new List<Customer>(); 
      CustomerName = new List<string>(); 
      CustomerLastName = new List<string>(); 
      CustomerName.Add("Name1"); 
      CustomerLastName.Add("LastName1"); 
      CustomerName.Add("Name2"); 
      CustomerLastName.Add("LastName2"); 

      Customers.Add(new Customer() { Name = CustomerName[0], LastName = CustomerLastName[0] }); 
      Customers.Add(new Customer() { Name = CustomerName[1], LastName = CustomerLastName[1] }); 
     } 
    } 
} 

**MainWindow.Xaml** 
<Window x:Class="BindingAnItemControlToAList.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:BindingAnItemControlToAList" 
     Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" > 
    <Window.Resources> 
     <local:CustomerList x:Key="Cust"/> 
    </Window.Resources> 
    <Grid Name="Grid1"> 
     <ListBox ItemsSource="{Binding Source={StaticResource Cust}}" DisplayMemberPath="CustomerName" Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates" VerticalAlignment="Top" Width="245" /> 
      </Grid> 
</Window> 

回答

4

Your're不设置一个“集合”作为ItemsSource ...因此你没有得到任何选择。这将选择列表CustomerName

<ListBox ItemsSource="{Binding Source={StaticResource Cust}, Path=CustomerName}" Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates" VerticalAlignment="Top" Width="245" /> 

不过说真的,你应该调整你CustomerList类...有没有需要有单独列出了复制CustomerNameLastName领域 - 这意味着你是不必要的重复数据,也有可能为数据与每个集合不同步。

你也应该考虑使用INotifyPropertyChanged的,和你的类使用INotifyCollectionChanged(例如使用ObservableCollection代替List,并在Customer类实现INotifyPropertyChanged)是否有可能改变集合或数据。

例如

public class CustomerList 
{ 
    public ObservableCollection<Customer> Customers { get; set; } 
    public CustomerList() 
    { 
     Customers = new ObservableCollection<Customer>(); 
     Customers.Add(new Customer { Name = "Name1", LastName = "LastName1" }); 
     Customers.Add(new Customer { Name = "Name2", LastName = "LastName2" }); 
    } 
} 

<ListBox ItemsSource="{Binding Source={StaticResource Cust}, Path=Customers}" DisplayMemberPath="Name" Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates" VerticalAlignment="Top" Width="245" /> 
+0

非常感谢,它为我工作。 – WpfBee

2

有几件事情在这里下车。首先,您的源设置为该对象,但实际列表是对象的属性Customers。所以你需要使用Source={StaticResource Cust},Path=Customers}。其次,DisplayMemberPath需要是该物品的属性,在这种情况下为Customer - 因此您必须使用名称而不是“客户名称”。这工作:

<ListBox 
    ItemsSource="{Binding Source={StaticResource Cust},Path=Customers}" 
    DisplayMemberPath="Name" /> 
+1

我不知道路径仍然可以与DisplayMemberPath一起用于ItemsSource。你的建议对我来说工作得很好。非常感谢。 – WpfBee

0

我最后拿本次讨论:要绑定,我们需要照顾3个属性的ItemControl:1.源 - 这将是X:键[类名]将包含集合属性(在我的情况,Cust)。 2.路径 - 它将是包含集合的属性(在我的情况下是客户)3. DisplayMemberPath - 要显示的实际属性(在我的情况下是Name)。

相关问题