DisplayMemberPath 在 WPF 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12994953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:52:27  来源:igfitidea点击:

DisplayMemberPath is not working in WPF

wpflistbox

提问by WpfBee

I want to display CustomerList\CustomerNameproperty items to the ListBoxusing ItemsSourceDisplayMemberPathproperty only. But it is not working. I do not want to use DataContextor any other binding in my problem. Please help. My code is given below:

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

MainWindow.xaml.cs

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

回答by Colin Smith

Your're not setting a "collection" as the ItemsSource...thus you don't get anything to choose from. This will choose the list CustomerName.

您没有将“集合”设置为 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" />

But really, you should restructure your CustomerListclass...there's no need to have separate lists that copy the Nameand LastNamefields 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.

但实际上,您应该重组您的CustomerList类......没有必要有单独的列表来复制NameLastName字段Customer- 这意味着您正在不必要地复制数据,并且数据也可能与每个集合不同步。

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

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

e.g.

例如

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

回答by McGarnagle

There are a couple of things off here. First, your source is set to the object, but the actual list is a property Customersof the object. So you need to use Source={StaticResource Cust},Path=Customers}. Second, the DisplayMemberPathneeds to be a property of the item, which in this case is Customer-- so you have to use Nameinstead of "CustomerName". This works:

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

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

回答by WpfBee

My final take from this discussion: To bind an ItemControl we need to take care of 3 properties: 1. Source - it will be the x:Key [class name] that will contain the collection property (in my case, Cust). 2. Path - it will be the property that contains collection (in my case, Customers) 3. DisplayMemberPath - the actual property that is to be displayed (in my case it is, Name).

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