为什么 checklistbox 没有数据源?如何绑定到值列表?

发布于 2024-12-17 07:15:06 字数 455 浏览 0 评论 0原文

我正在开发一个 Winform,我需要一个复选框。我将值存储在具有 List 属性的对象中:

public static class Fields
{
    public static IList<string> FieldList { get; set; }

    static Fields()
    { ...//populate FieldList }
}

现在我希望我的 CheckedListBox 使用 Fields.FieldList 作为数据源。在线搜索后,我发现我需要设置

//in myForm_Load
mycheckedListBox.DataSource = Fields.FieldList;

But myCheckedListBox 没有 DataSource 属性。

我在这里错过了什么吗?

I am developing a Winform and I need a checkedlistbox. I have the values stored in an object which has a List property:

public static class Fields
{
    public static IList<string> FieldList { get; set; }

    static Fields()
    { ...//populate FieldList }
}

Now I would like my CheckedListBox to use Fields.FieldList as datasource. After searching online I found I needed to set

//in myForm_Load
mycheckedListBox.DataSource = Fields.FieldList;

But myCheckedListBox does not have a DataSource property.

Am I missing something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一城柳絮吹成雪 2024-12-24 07:15:06

根据文档,它应该具有此属性... http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.datasource(VS.90).aspx

但是,我也有同样的不久前在一个项目上遇到问题,并使用这篇 CodeProject 文章 编写了解决方案在我需要此功能的一个项目中。

研究了一下,我确实发现了这个:

http://connect.microsoft.com/VisualStudio/feedback/details/115199/checkedlistbox-datasource-displaymember-valuemember-are-hidden

编辑:上面的链接不再有效,但下面的摘录来自曾经驻留在那里的文章。

发布者:Microsoft 于 2005 年 5 月 30 日上午 10:28
感谢您的反馈
但这是设计使然。我们不支持数据绑定
CheckedListBox 控件。这些属性是从它的基础继承的
类并且无法删除,因此我们将它们隐藏在属性网格中并且
智能感知。

这解释了为什么该属性存在,但没有在 Intellisense 中显示。

这篇博文也值得一读: http://waxtadpole.wordpress.com/2009/10/12/assigning-custom-class-to-checkedlistbox-datasource/

Per the documentation, it should have this property... http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.datasource(VS.90).aspx

However, I also had the same issue on a project a while back, and used this CodeProject article to code the solution in the one project where I needed this feature.

Researching a bit more, I did find this:

http://connect.microsoft.com/VisualStudio/feedback/details/115199/checkedlistbox-datasource-displaymember-valuemember-are-hidden

Edit: The above link is no longer working, but the exceprt below is from the article that once resided there.

Posted by Microsoft on 5/30/2005 at 10:28 AM
Thanks for the feedback
however this is by design. We do not support data binding on the
CheckedListBox control. These properties are inherited from it base
class and cannot be removed so we hid them form the property grid and
IntelliSense.

That explains why the property exists, but doesn't show in Intellisense.

This blog post is worth a read as well: http://waxtadpole.wordpress.com/2009/10/12/assigning-custom-class-to-checkedlistbox-datasource/

爱的故事 2024-12-24 07:15:06

以下是我将 User 对象的 List 绑定到 CheckedListBox 的方法。

((ListBox)myCheckedListBox).DataSource = listOfUsers;
((ListBox)myCheckedListBox).DisplayMember = "FullName";
((ListBox)myCheckedListBox).ValueMember = "UserID";

当然不建议这样做,因为文档告诉我们该属性是隐藏的。

上面的代码可以工作,但我注意到 Visual Studio 2012 中存在一些副作用,例如:

渲染选中标记的延迟:

单击所需的项目后,渲染“选中”标记会出现烦人的延迟。

就我而言,CheckOnClick 属性为 True,CausesValidation 为 False。

Here is how I am binding a List<T> of User objects into CheckedListBox.

((ListBox)myCheckedListBox).DataSource = listOfUsers;
((ListBox)myCheckedListBox).DisplayMember = "FullName";
((ListBox)myCheckedListBox).ValueMember = "UserID";

Of course this is not recommended, since documentation is telling us that this property is hidden.

The code above works, but I noticed some side effects in Visual Studio 2012 such as:

Delay for rendering checked marker:

After you click on the desired item, there is annoying delay to render the "checked" marker.

In my case, CheckOnClick property is True, CausesValidation is False.

最冷一天 2024-12-24 07:15:06

就我个人而言,我使用绑定到 DataTableDataGridView,该 DataTable 具有布尔字段以及显示值字段。

如果您隐藏列标题和行标题,那么您将获得与 CheckedListBox 所提供的非常接近的内容。

Personally I use a DataGridView that is bound to a DataTable that has a Boolean field along with a field for the display value.

If you hide the column headers and row headers then you get something pretty close to what a CheckedListBox gives you.

世态炎凉 2024-12-24 07:15:06

可以通过迭代您的潜在数据源并一次添加其项目来解决此问题。例如:

这样会引发异常:

myCheckedListBox.DataSource = myStringList;

可以修改为这样:

foreach (string myString in myStringList)
{
    myCheckedListBox.Items.Add(myString);
}

This can be worked around by iterating through your would-be datasource and adding its items one-at-a-time. For example:

This, which will cause an exception:

myCheckedListBox.DataSource = myStringList;

Can be modified to this:

foreach (string myString in myStringList)
{
    myCheckedListBox.Items.Add(myString);
}
分分钟 2024-12-24 07:15:06

我通过将 ToString () 方法更改为应该出现的名称解决了这个问题:

public static class Fields
{
   public string MyDisplayMenber{ get; set; }

   public override string ToString(){
         return MyDisplayMenber;
   }
}

并变成带有对象的数组列表:

{
  mycheckedlistbox.Items.AddRange(MyList.ToArray<Fields>());
}

I solved the problem by changing the ToString () method to the name that should appear:

public static class Fields
{
   public string MyDisplayMenber{ get; set; }

   public override string ToString(){
         return MyDisplayMenber;
   }
}

and turning into array list with objects:

{
  mycheckedlistbox.Items.AddRange(MyList.ToArray<Fields>());
}
淡水深流 2024-12-24 07:15:06

我知道这已经很老了;为了任何仍然有相同要求的人的利益,这对我有用。请注意,我没有使用 DisplayMemberValueMember 属性,因为它似乎是不鼓励的(请参阅上面@David Stratton 的帖子)。

//References
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/0e0da0c9-299e-46d0-b8b0-4ccdda15894c/how-to-assign-values-to-checkedlistbox-items-and-sum-these-values?forum=csharpgeneral


using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MyApp
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        DatabaseProviderFactory factory = new DatabaseProviderFactory(); 
        Database db = factory.Create("MyConnString");
        DataTable dt = db.ExecuteDataSet(CommandType.StoredProcedure, "ProcGetCustomers").Tables[0];
        var custlist = new List<CheckBoxItem<string, string>>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            custlist.Add(Create(dt.Rows[i]["DESC"].ToString(), dt.Rows[i]["CODE"].ToString()));
        }
        checkedListBox1.Items.AddRange(custlist.Cast<object>().ToArray());


   }
    public class CheckBoxItem<K, V>
    {
        public CheckBoxItem(K displayValue, V hiddenValue)
        {
            DisplayValue = displayValue;
            HiddenValue = hiddenValue;
        }

        public K DisplayValue { get; private set; }
        public V HiddenValue { get; private set; }


        public override string ToString()
        {
            return DisplayValue == null ? "" : DisplayValue.ToString();
        }
    }
    public static CheckBoxItem<K, V> Create<K, V>(K displayValue, V hiddenValue)
    {
        return new CheckBoxItem<K, V>(displayValue, hiddenValue);
    }
  }
}

I know this is pretty old; for the benefit of anyone who still has the same requirement, here's what worked for me. Note that I did not use DisplayMember or ValueMember properties, as it seems like it is discouraged (see @David Stratton's post above).

//References
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/0e0da0c9-299e-46d0-b8b0-4ccdda15894c/how-to-assign-values-to-checkedlistbox-items-and-sum-these-values?forum=csharpgeneral


using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace MyApp
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        DatabaseProviderFactory factory = new DatabaseProviderFactory(); 
        Database db = factory.Create("MyConnString");
        DataTable dt = db.ExecuteDataSet(CommandType.StoredProcedure, "ProcGetCustomers").Tables[0];
        var custlist = new List<CheckBoxItem<string, string>>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            custlist.Add(Create(dt.Rows[i]["DESC"].ToString(), dt.Rows[i]["CODE"].ToString()));
        }
        checkedListBox1.Items.AddRange(custlist.Cast<object>().ToArray());


   }
    public class CheckBoxItem<K, V>
    {
        public CheckBoxItem(K displayValue, V hiddenValue)
        {
            DisplayValue = displayValue;
            HiddenValue = hiddenValue;
        }

        public K DisplayValue { get; private set; }
        public V HiddenValue { get; private set; }


        public override string ToString()
        {
            return DisplayValue == null ? "" : DisplayValue.ToString();
        }
    }
    public static CheckBoxItem<K, V> Create<K, V>(K displayValue, V hiddenValue)
    {
        return new CheckBoxItem<K, V>(displayValue, hiddenValue);
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文