DisplayMemberPath在WPF中不起作用 [英] DisplayMemberPath is not working in WPF

查看:67
本文介绍了DisplayMemberPath在WPF中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想使用 ItemsSource DisplayMemberPath 属性将 CustomerList \ CustomerName 属性项显示到 ListBox .但这是行不通的.我不想在我的问题中使用 DataContext 或任何其他绑定.请帮忙.我的代码如下:

I want to display CustomerList\CustomerName property items to the ListBox using ItemsSource DisplayMemberPath property only. But it is not working. I do not want to use DataContext or any other binding in my problem. Please help. My code is given below:

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>

推荐答案

您没有将集合"设置为ItemsSource ...因此您没有任何选择.这将选择列表 CustomerName .

Your're not setting a "collection" as the ItemsSource...thus you don't get anything to choose from. This will choose the list 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 类...不需要单独的列表来复制 Name LastName 字段 Customer (客户)的代码-这意味着您不必要地复制数据,并且数据有可能与每个集合不同步.

But really, you should restructure your CustomerList class...there's no need to have separate lists that copy the Name and LastName fields of the Customer - it means you are duplicating data unnecessarily, and also it's possible for the data to get out of sync with each collection.

您还应该考虑使用 INotifyPropertyChanged,并在您的类上使用 INotifyCollectionChanged(例如,使用 ObservableCollection 而不是 List,并在类上实现 INotifyPropertyChanged Customer 类),如果有可能更改集合或数据.

You also should consider using INotifyPropertyChanged, and use INotifyCollectionChanged on your classes (e.g. use ObservableCollection instead of List, and implement INotifyPropertyChanged on the Customer class) if it's possible to change the collection or data.

例如

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" />

这篇关于DisplayMemberPath在WPF中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆