2

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

主窗口.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

3 回答 3

4

您没有将“集合”设置为 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类......没有必要有单独的列表来复制NameLastName字段Customer- 这意味着你在不必要地复制数据,而且数据也有可能与每个集合不同步。

如果可以更改集合或数据,您还应该考虑使用 INotifyPropertyChanged,并在您的类上使用 INotifyCollectionChanged(例如,使用ObservableCollection代替List,并INotifyPropertyChanged在类上实现)。Customer

例如

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" />
于 2012-10-21T04:49:31.167 回答
2

这里有几件事。首先,您的源设置为对象,但实际列表是Customers对象的属性。所以你需要使用Source={StaticResource Cust},Path=Customers}. 其次,DisplayMemberPath需要是项目的一个属性,在这种情况下是Customer——所以你必须使用名称而不是“客户名称”。这有效:

<ListBox 
    ItemsSource="{Binding Source={StaticResource Cust},Path=Customers}"  
    DisplayMemberPath="Name" />
于 2012-10-21T04:48:18.990 回答
0

我从这个讨论中得到的最后一点:要绑定一个 ItemControl,我们需要处理 3 个属性: 1. Source - 它将是 x:Key [类名],它将包含集合属性(在我的例子中是 Cust)。2. 路径 - 它将是包含集合的属性(在我的例子中是客户) 3. DisplayMemberPath - 要显示的实际属性(在我的例子中是名称)。

于 2013-03-06T16:31:26.133 回答