解决解决SLOVEDWPF数据绑定到泛型列表<>
仿制药清单结构列表
如果你使用的,而不是一个结构的一类,它工作正常
即
public class Person
{
public string Name { get; set;}
public string ID { get; set;}
}
instead of
public struct Person
{
public string Name;
public string ID;
}
我在做错事..你知道它是怎么回事。
我试过玩弄ItemsSource,DataContext,DisplayMemberPath和SelectedValuePath,我要么在Person对象中调用ToString方法列表的空白列表;
什么是真正的帮助是有人发布一个适用于这个例子的答案。
我已经简化了这个问题,因为我在使用数据绑定泛型时遇到了一些困难。
我已经创建了一个简单的通用人员列表,并希望将其绑定到组合。 (也想尝试使用ListView)。
我要么得到毛坯列表或“xxxx.Person”的名单,其中xxxx =显示命名空间
<Window x:Class="BindingGenerics.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300">
<Grid>
<ComboBox Name="ComboBox1"
ItemsSource="{Binding}"
Height="50"
DisplayMemberPath="Name"
SelectedValuePath="ID"
FontSize="14"
VerticalAlignment="Top">
</ComboBox>
</Grid>
</Window>
using System.Windows;
using System.ComponentModel;
namespace BindingGenerics
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Person p = new Person();
// I have tried List and BindingList
//List<Person> list = new List<Person>();
BindingList<Person> list = new BindingList<Person>();
p.Name = "aaaa";
p.ID = "1111";
list.Add(p);
p = new Person();
p.Name = "bbbb";
p.ID = "2222";
list.Add(p);
p = new Person();
p.Name = "cccc";
p.ID = "3333";
list.Add(p);
p = new Person();
p.Name = "dddd";
p.ID = "4444";
list.Add(p);
ComboBox1.DataContext = list;
}
}
public struct Person
{
public string Name;
public string ID;
}
}
我试过你的示例,它适用于我。除了多次显示dddd。 – 2010-02-16 04:47:21