zoukankan      html  css  js  c++  java
  • WPF 控件之ComboBox绑定

    直接粘过来一个方法。。

    WPF中提供了数据绑定的功能,操作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面操作一下下拉列表框控件ComboBox控件的数据绑定操作。

    要绑定到ComboBox控件的自定义类:

    public class LocationRoad
    {
        public int ID { setget; }
        public string Code { setget; }
        public string Info { setget; }
    }

    建立数据源,我们就将此数据集合当作数据源绑定到ComboBox:

    ///
    /// 当ComboBox选中项更改时发生
    ///
    private LocationRoad _selectLocation;
    public LocationRoad SelectLocation
    {
        get
        {
            return this._selectLocation;
        }
        set
        {
            this._selectLocation = value;
            if (this.PropertyChanged != null)
                PropertyChanged(thisnew PropertyChangedEventArgs("SelectLocation"));
        }
    }
     
    private ObservableCollection _locationRoad = null;
     
    public ObservableCollection LocationSource
    {
        get
        {
            if (this._locationRoad == null)
            {
                this._locationRoad = new ObservableCollection() {
                     new LocationRoad() { ID = 1, Code = "NGQ", Info = "南岗区" },
                     new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里区" },
                     new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外区" },
                     new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房区" },
                     new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊区" },
                     };
     
            }
            return this._locationRoad;
        }
        set
        {
            this._locationRoad = value;
            if (this.PropertyChanged != null)
                PropertyChanged(thisnew PropertyChangedEventArgs("LocationSource"));
        }
    }

    前台XAML文件绑定方式:

    <ComboBox Margin="-16,3,0,5" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Name="cboxLocationKeyword"
     ItemsSource="{Binding LocationSource,Mode=OneWay}"   --->单项绑定数据源
     SelectedValuePath="ID"     --->这个是选中后的值,应该就是SelectedValue
     DisplayMemberPath="Info"   --->这个是显示的text
     SelectedItem="{Binding SelectLocation}" />

    如果要进行双向绑定或其他的绑定方式,只要更改上面binging块中的Mode方式就可以了。

  • 相关阅读:
    [LeetCode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
    [LeetCode] 103. Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
    Notepad++ Shortcuts 快捷键
    IplImage 与 QImage 相互转换
    [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
    QMessageBox 使用方法
    Qt5 和 Qt4 的一些改动和不同
    Qt5.4 VS2010 Additional Dependancies
    [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
    [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
  • 原文地址:https://www.cnblogs.com/mantian/p/2871775.html
Copyright © 2011-2022 走看看