C#:如何绑定到ListBox DisplayMember和ValueMember类方法的结果? [英] C#: How to bind to ListBox DisplayMember and ValueMember result from class method?

查看:609
本文介绍了C#:如何绑定到ListBox DisplayMember和ValueMember类方法的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建列表框,我将有键值对。这些数据是我从getter提供的类中获得的。



类:

  public class myClass 
{
private int key;
私有字符串值;

public myClass(){}
$ b public int GetKey()
{
return this.key;
}

public int GetValue()
{
return this.value;


code


$ p

 私人列表< myClass>我的列表; 
$ b $ public void Something()
{
myList = new myList< myClass>();

//代码填入myList

this.myListBox.DataSource = myList;
this.myListBox.DisplayMember = ??; //想要类似myList.Items.GetValue()
this.myListBox.ValueMember = ??; //想要类似myList.Items.GetKey()
this.myListBox.DataBind();

$ / code>

与此主题相似[

是否可以做一些简单的事情,或者我最好重新完成思想流程(和这个解决方案)?



感谢您的建议!

解决方案

DisplayMember ValueMember 属性需要使用属性的名称(作为字符串)。你不能使用一种方法。所以你有两个选择。改变你的类来返回属性或者创建一个派生自myClass的类,你可以在其中添加两个缺失的属性

  public class myClass2:myClass 
{

public myClass2(){}
$ b $ public int MyKey
{
get {return base.GetKey();}
set {base.SetKey(value);}
}

public string MyValue
{
get {return base.GetValue();}
set {base.SetValue(value);}
}
}

现在你已经做出了这些改变,你可以用新类改变你的列表(但是修复初始化)

pre $ //这里你声明一个myClass元素的列表
private List< myClass2>我的列表;

public void Something()
{
//在这里初始化一个myClass元素列表
myList = new List< myClass2>();

//代码填充myList
myList.Add(new myClass2(){MyKey = 1,MyValue =Test});

myListBox.DataSource = myList;
myListBox.DisplayMember =MyKey; //只需设置属性的正确名称
myListBox.ValueMember =MyValue;
this.myListBox.DataBind();
}


I'm trying to create ListBox where I will have key-value pair. Those data I got from class which provides them from getters.

Class:

public class myClass
{
    private int key;
    private string value;

    public myClass() { }

    public int GetKey()
    {
        return this.key;
    }

    public int GetValue()
    {
        return this.value;
    }
}

Program:

private List<myClass> myList;

public void Something()
{
    myList = new myList<myClass>();

    // code for fill myList

    this.myListBox.DataSource = myList;
    this.myListBox.DisplayMember = ??; // wanted something like myList.Items.GetValue()
    this.myListBox.ValueMember = ??; // wanted something like myList.Items.GetKey()
    this.myListBox.DataBind();
}

It's similar to this topic [ Cannot do key-value in listbox in C# ] but I need to use class that returns values from methods.

Is it possible to do somewhat simple or I'd better rework my thought flow (and this solution) completely?

Thank you for advice!

解决方案

The DisplayMember and ValueMember properties require the name (as a string) of a property to be used. You can't use a method. So you have two options. Change you class to return properties or make a class derived from myClass where you could add the two missing properties

public class myClass2 : myClass
{

    public myClass2() { }

    public int MyKey
    {
        get{ return base.GetKey();}
        set{ base.SetKey(value);}
    }

    public string MyValue
    {
        get{return base.GetValue();}
        set{base.SetValue(value);}
    }
}

Now that you have made these changes you could change your list with the new class (but fix the initialization)

// Here you declare a list of myClass elements
private List<myClass2> myList;

public void Something()
{
    // Here you initialize a list of myClass elements
    myList = new List<myClass2>();

    // code for fill myList
    myList.Add(new myClass2() {MyKey = 1, MyValue = "Test"});

    myListBox.DataSource = myList;
    myListBox.DisplayMember = "MyKey"; // Just set the correct name of the properties 
    myListBox.ValueMember = "MyValue"; 
    this.myListBox.DataBind();         
}

这篇关于C#:如何绑定到ListBox DisplayMember和ValueMember类方法的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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