WPF数据绑定到泛型 [英] WPF Databinding to Generics

查看:473
本文介绍了WPF数据绑定到泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做错了..你知道怎么回事.

我已经简化了该问题,因为我通常在使用数据绑定泛型时遇到困难.

我创建了一个简单的人员通用列表",并希望将其绑定到组合. (也想尝试使用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}"
身高="50"
DisplayMemberPath =名称"
SelectedValuePath ="ID"
FontSize ="14"
VerticalAlignment ="Top">
</ComboBox>

</Grid>
</Window>

使用System.Windows;
使用System.ComponentModel;
命名空间BindingGenerics
{
///< summary>
///MainWindow.xaml的交互逻辑
///</summary>
公共局部类MainWindow:Window
{
公共MainWindow()
{
InitializeComponent();
人p =新的Person();
//我已经尝试过List和BindingList
//List< Person> list = new List< Person>();
BindingList< Person> list = new BindingList< Person>();
p.Name ="aaaa";
p.ID ="1111";
list.Add(p);
p =新的Person();
p.Name ="bbbb";
p.ID ="2222";
list.Add(p);
p =新的Person();
p.Name ="cccc";
p.ID ="3333";
list.Add(p);
p =新的Person();
p.Name ="dddd";
p.ID ="4444";
list.Add(p);
ComboBox1.DataContext =列表;
}
}
公共结构人
{
公共字符串名称;
公共字符串ID;
}

I am doing something wrong .. you know how it is.

I have simplified the problem as I am having difficulty in general with databinding generics.

I have created a simple Generic List of Person and want to bind it to a combo. (also want to try use a ListView too).

I either get a list of blanks or a list of ''xxxx.Person'' where xxxx = namespace

<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;
}

推荐答案

您需要为列表框创建一个模板,以便它知道如何显示人员对象.如果您不告诉它,它将调用ToString()并获取类名.某些控件具有DisplayMember属性,您可以将其设置为要显示的属性,但是我认为这不会在WPF中发生.
You need to create a template for your listbox, so that it knows how to display a person object. If you don''t tell it, it calls ToString() and gets back the class name. Some controls have a DisplayMember property which you set to the property you want to show, but I don''t think that happens in WPF.


为Person类/结构发布代码.如果它是一类,则看起来您是在反复将同一个人添加到列表中(而不是每次都创建一个新实例).
Post your code for your Person class/struct. If it is a class, it looks like you are adding the same person to the list repeatedly (rather than creating a new instance each time).


示例中有一个错误.我实际上是在更新每个人..只是在这里写示例的一个错误..谢谢


已解决已解决的问题

泛型列表是结构列表

如果您使用类而不是结构,则效果很好



公共类Person
{
公共字符串名称{get;设置;}
公共字符串ID {get;设置;}
}
代替

公共结构人
{
公共字符串名称;
公共字符串ID;
}
There was an error in the example .. I was actually newing each Person .. just an error in writing eample here.. thanks


SOLVED SOLVED SLOVED

the generics list was a list of structs

if you use a class instead of a struct it works fine

i.e.

public class Person
{
public string Name { get; set;}
public string ID { get; set;}
}
instead of

public struct Person
{
public string Name;
public string ID;
}


这篇关于WPF数据绑定到泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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