gpt4 book ai didi

wpf - DisplayMemberPath 在 WPF 中不起作用

转载 作者:行者123 更新时间:2023-12-04 16:16:21 25 4
gpt4 key购买 nike

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

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

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

关于wpf - DisplayMemberPath 在 WPF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12994953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com