zoukankan      html  css  js  c++  java
  • 绑定CheckedListBox

    介绍 看起来。net CheckedListBox在ListBox和ComboBox中甚至没有主绑定工具,因为它必须在数据库中保存多个值。这篇文章介绍了一个扩展的CheckedListBox,它有一个单独的属性来设置和获取被选中的项,也有绑定工具。 问题 . net CheckedListBox没有绑定工具。 因为CheckedListBox与对象集合一起工作,所以它不能绑定到数据源。 CheckedListBox没有一个属性来表示项目是否被选中并且它必须被更改,或者很难通过一个项目或一组被选中项目的方法来获取数据。 需要 创建表示已选中项并基于其设置已选中项的公共可绑定属性。 它必须是一个属性,在设置或获取时保存单个原语类型值。 项目必须从与值数据源不同的数据源(可以是查找表)加载。 由于列表控件的性质,我们必须将其绑定到列。 使用的代码 关于如何使用文章或代码的简要描述。类名,方法和属性,任何技巧或提示。 ExCheckedListBox 在代码示例中,主要代码属于ExCheckedListBox继承自。net框架的CheckedListBox。 它扩展了CheckedListBox控件,有三个额外的属性: 1. 属性的值: 此属性是整数类型,允许基于单个值获取和设置已检查项。它使用该整数中的位,保存和检索单个列中的值。隐藏,收缩,复制Code

    get
    {
        ///Gets checked items in decimal mode from binary mode
        
        try
        {
            //each item in list has a number that is binary number in decimal mode
            //this number represents that number
            int poweredNumber = 1;
            //loop in all items of list
            for (int i = 0; i < this.Items.Count; i++)
            {
                //if item checked and the value doesn't contains poweredNumber 
                //then add poweredNumber to the value
                if((this.GetItemChecked(i)))
                    this.value |= poweredNumber;
                //else if poweredNumber exists in the value remove from it
                else if ((this.value & poweredNumber) != 0)
                    this.value -= poweredNumber;
    
                //raise to the power
                poweredNumber *= 2;
            }
        }
        catch (ArgumentException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
    
        return this.value;
    }
    set
    {
        ///sets checked items from binary mode converted from decimal value
    
        this.value = value;
        try
        {
            //each item in list has a number that is binary number in decimal mode
            //this number represents that number
            int poweredNumber = 1;
            //loop in all items of list
            for (int i = 0; i < this.Items.Count; i++)
            {
                //if poweredNumber exists in the value set checked on item
                if ((this.value & poweredNumber) != 0)
                    this.SetItemCheckState(i, CheckState.Checked);
                //else remove checked from item
                else
                    this.SetItemCheckState(i, CheckState.Unchecked);
    
                //raise to the power
                poweredNumber *= 2;
            }
        }
        catch (ArgumentException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
    
    }

    2. DataSource属性: 此属性是一种对象类型,可以像其他集合控件(ListBox和ComboBox)一样获取和设置数据源。实际上它使用的是基本的数据源属性,但是由于CheckedListBox逻辑,它隐藏在。net CheckedListBox中。因此,我通过new关键字隐藏它并重用基础数据源保护属性。隐藏,收缩,复制Code

    /// <summary>
    /// Gets or sets the data source for this CustomControls.CheckedListBox.
    /// Returns:
    ///    An object that implements the System.Collections.IList or 
    //     System.ComponentModel.IListSource
    ///    interfaces, such as a System.Data.DataSet or an System.Array. The <BR>///    default is null.
    //////Exceptions:
    ///  System.ArgumentException:
    ///    The assigned value does not implement the System.Collections.IList or 
    //     System.ComponentModel.IListSource
    ///    interfaces.
    /// </summary>
    [DefaultValue("")]
    [AttributeProvider(typeof(IListSource))]
    [RefreshProperties(RefreshProperties.All)]
    [Browsable(true)]
    public new object DataSource { 
        get 
        {
            return base.DataSource;
        }
        set 
        {
            base.DataSource = value;
            
        }
    }

    3.DisplayMember属性: 这个属性是一种字符串类型,可以像其他集合控件(ListBox和ComboBox)一样获取和设置数据源特定列。 实际上它使用了基本的显示成员属性,但是由于CheckedListBox逻辑,它隐藏在。net CheckedListBox中。 所以我通过new关键字隐藏它并重用了base DisplayMember保护属性。隐藏,复制Code

    /// <summary>
    ///   Gets or sets the property to display for this <BR>///   CustomControls.CheckedListBox.
    ////// Returns:
    ///     A System.String specifying the name of an object property that is <BR>///     contained in the collection specified by the <BR>///     CustomControls.CheckedListBox.DataSource property. The default is <BR>///     an empty string ("").
    /// </summary>
    [DefaultValue("")]
    [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, <BR>System.Design, 
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    [Editor("System.Windows.Forms.Design.DataMemberFieldEditor, System.Design, 
    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", 
    typeof(UITypeEditor))]
    [Browsable(true)]
    public new string DisplayMember
    {
        get
        {
            return base.DisplayMember;
        }
        set
        {
            base.DisplayMember = value;
            
        }
    }

    演示项目 演示项目包含一个SQL脚本,创建两个表: 具有相同场景的产品和城市(指定城市中呈现的产品)。 的兴趣点 我想实现另一个场景(问题和答案),在这个场景中,我们必须导航问题并使用多个答案回答测试,但在数据绑定和这个场景中存在一个问题。 本文转载于:http://www.diyabc.com/frontweb/news230.html

  • 相关阅读:
    angular
    客户端存储cookie ---(优缺点及定义及用途)
    cookie的设置和获取
    和谐敏感字
    移动端上滑下滑换图片
    移动端适配方式
    Viewport及判断移动端上下滑动
    HTML5拖放&地理定位
    用canvas 做一个钟表
    用canvas上传图片
  • 原文地址:https://www.cnblogs.com/Dincat/p/13431163.html
Copyright © 2011-2022 走看看