• 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);
                }
            }
        }
    }
  • 相关阅读:
    JSON字符串转对象,List集合,需要的jar 包
    VirtualBox 更改虚拟磁盘大小
    SpringBoot webSocket 发送广播、点对点消息,Android接收
    window系统下添加 glassfish 的系统服务
    idea把java web项目打成war包
    栈类型数据的运用
    leetcode实践:找出两个有序数组的中位数
    leetcode实践:通过链表存储两数之和
    通过监控Nginx日志来实时屏蔽高频恶意访问的IP
    Java版分布式ID生成器技术介绍
  • 原文地址:https://www.cnblogs.com/ChangTan/p/9855491.html
Copyright © 2020-2023  润新知