• C# Wpf集合双向绑定


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

    前台代码

    <StackPanel Name="PriceSet" Margin="0,10,0,0" Visibility="Visible">
                    <ListBox HorizontalAlignment="Center" Margin="21,12,0,0" Name="listBox_Price" VerticalAlignment="Top" Height="auto" Width="auto" MinHeight="200" MinWidth="350">
                        <ListBox.Template>
                            <ControlTemplate TargetType="{x:Type ListBox}">
                                <WrapPanel Orientation="Horizontal" IsItemsHost="True"/>
                            </ControlTemplate>
                        </ListBox.Template>
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="游戏时长:" Tag="{Binding Path=Price_ID}" Margin="10,10"/>
                                    <TextBox Width="60" Text="{Binding Path=TimeLong}" Margin="10,10"/>
                                    <TextBlock Text="支付人民币:" Margin="10,10"/>
                                    <TextBox Width="60" Text="{Binding Path=PayPrice}" Margin="10,10"/>
                                    <TextBlock Text="投币数量:" Margin="10,10"/>
                                    <TextBox Width="60" Text="{Binding Path=CoinNum}" Margin="10,10"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox >
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="20,20">
                        <Button Name="Price_Add" Content="添加" Margin="10,10" Width="100" Height="25"/>
                        <Button Name="Price_Delete" Content="删除" Margin="10,10" Width="100" Height="25"/>
                        <Button Name="Price_Save" Content="保存" Margin="10,10" Width="100" Height="25" Click="Price_Save_Click"/>
                    </StackPanel>
                </StackPanel>

    后台代码:

    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
    
            /// <summary>
            /// 价格ID
            /// </summary>
            private int _Price_ID;
            /// <summary>
            /// 游戏时长
            /// </summary>
            private int _TimeLong;
            /// <summary>
            /// 支付价格
            /// </summary>
            private float _PayPrice;
            /// <summary>
            /// 投币数量
            /// </summary>
            private int _CoinNum;
            /// <summary>
            /// 是否被选中
            /// </summary>
            private bool _bSelected = false;
            /// <summary>
            /// 价格列表
            /// </summary>
            private static ObservableCollection<Price> _PriceList;
    
            #endregion
    
            #region Property
    
            /// <summary>
            /// 价格ID
            /// </summary>
            public int Price_ID
            {
                get { return _Price_ID; }
                set { _Price_ID = value; MyPropertyChanged(nameof(Price_ID)); }
            }
            /// <summary>
            /// 游戏时长
            /// </summary>
            public int TimeLong
            {
                get { return _TimeLong; }
                set { _TimeLong = value; MyPropertyChanged(nameof(TimeLong)); }
            }
            /// <summary>
            /// 支付价格
            /// </summary>
            public float PayPrice
            {
                get { return _PayPrice; }
                set { _PayPrice = value; MyPropertyChanged(nameof(PayPrice)); }
            }
            /// <summary>
            /// 投币数量
            /// </summary>
            public int CoinNum
            {
                get { return _CoinNum; }
                set { _CoinNum = value; MyPropertyChanged(nameof(CoinNum)); }
            }
            /// <summary>
            /// 是否被选中
            /// </summary>
            public bool bSelected
            {
                get { return _bSelected; }
                set { _bSelected = value; MyPropertyChanged(nameof(bSelected)); }
            }
            /// <summary>
            /// 价格列表
            /// </summary>
            public static ObservableCollection<Price> 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<Price>();
                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);
                }
            }
        }
    }
  • 相关阅读:
    要学习的东西~!~
    Concrete5官网
    PHP免费源码下载
    PHP源码下载1
    PHP源码下载
    PHP框架之ThinkPHP项目CMS内容管理系统源码及开发手册视频
    SELINUX导致数据修改权限不成功
    mysql参数安全设置
    大话存储(1-5)
    存储知识(4)
  • 原文地址:https://www.cnblogs.com/ChangTan/p/9855491.html
Copyright © 2020-2023  润新知