解決解決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