C# Wpf集合双向绑定

2021-07-04 13:20

阅读:396

标签:pre   site   string   mod   table   ack   att   target   tar   

ObservableCollection 用于同步更新集合,删除集合内的元素,界面也会同步删除,

前台代码

"PriceSet" Margin="0,10,0,0" Visibility="Visible">
                "Center" Margin="21,12,0,0" Name="listBox_Price" VerticalAlignment="Top" Height="auto" Width="auto" MinHeight="200" MinWidth="350">
                    "{x:Type ListBox}">
                            "Horizontal" IsItemsHost="True"/>
                        "Horizontal">
                                "游戏时长:" Tag="{Binding Path=Price_ID}" Margin="10,10"/>
                                "60" Text="{Binding Path=TimeLong}" Margin="10,10"/>
                                "支付人民币:" Margin="10,10"/>
                                "60" Text="{Binding Path=PayPrice}" Margin="10,10"/>
                                "投币数量:" Margin="10,10"/>
                                "60" Text="{Binding Path=CoinNum}" Margin="10,10"/>
                            "Horizontal" HorizontalAlignment="Center" Margin="20,20">
                    

后台代码:

this.listBox_Price.ItemsSource = Price.PriceList;
MyPropertyChanged用于同步类发生
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Collections.ObjectModel;

namespace Jas.Remoting.SqliteHelper
{
    [Serializable]
    public class Price : INotifyPropertyChanged
    {
        #region Field

        /// 
        /// 价格ID
        /// 
        private int _Price_ID;
        /// 
        /// 游戏时长
        /// 
        private int _TimeLong;
        /// 
        /// 支付价格
        /// 
        private float _PayPrice;
        /// 
        /// 投币数量
        /// 
        private int _CoinNum;
        /// 
        /// 是否被选中
        /// 
        private bool _bSelected = false;
        /// 
        /// 价格列表
        /// 
        private static ObservableCollection _PriceList;

        #endregion

        #region Property

        /// 
        /// 价格ID
        /// 
        public int Price_ID
        {
            get { return _Price_ID; }
            set { _Price_ID = value; MyPropertyChanged(nameof(Price_ID)); }
        }
        /// 
        /// 游戏时长
        /// 
        public int TimeLong
        {
            get { return _TimeLong; }
            set { _TimeLong = value; MyPropertyChanged(nameof(TimeLong)); }
        }
        /// 
        /// 支付价格
        /// 
        public float PayPrice
        {
            get { return _PayPrice; }
            set { _PayPrice = value; MyPropertyChanged(nameof(PayPrice)); }
        }
        /// 
        /// 投币数量
        /// 
        public int CoinNum
        {
            get { return _CoinNum; }
            set { _CoinNum = value; MyPropertyChanged(nameof(CoinNum)); }
        }
        /// 
        /// 是否被选中
        /// 
        public bool bSelected
        {
            get { return _bSelected; }
            set { _bSelected = value; MyPropertyChanged(nameof(bSelected)); }
        }
        /// 
        /// 价格列表
        /// 
        public static ObservableCollection PriceList
        {
            get { return _PriceList; }
            set { _PriceList = value; }
        }

        #endregion

        #region Event

        [field: NonSerializedAttribute()]
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Interface Implementation

        private void MyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        #endregion


        public static void CreatePriceList(DataTable dt)
        {
            _PriceList = new ObservableCollection();
            foreach(DataRow dr in dt.Rows)
            {
                Price price = new Price();
                price._Price_ID= Convert.ToInt32(dr[Field.Price_ID]);
                price._TimeLong = Convert.ToInt32(dr[Field.TimeLong]);
                price._PayPrice = Convert.ToInt32(dr[Field.Price]);
                price._CoinNum = Convert.ToInt32(dr[Field.CoinNum]);
                _PriceList.Add(price);
            }
        }
    }
}

 

C# Wpf集合双向绑定

标签:pre   site   string   mod   table   ack   att   target   tar   

原文地址:https://www.cnblogs.com/ChangTan/p/9855491.html


评论


亲,登录后才可以留言!