首页 / C# / C# Wpf集合双向绑定
C# Wpf集合双向绑定
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# Wpf集合双向绑定,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3751字,纯文字阅读大概需要6分钟。
内容图文
![C# Wpf集合双向绑定](/upload/InfoBanner/zyjiaocheng/849/fedac7222b7442828447d868980038ed.jpg)
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); } } } }
内容总结
以上是互联网集市为您收集整理的C# Wpf集合双向绑定全部内容,希望文章能够帮你解决C# Wpf集合双向绑定所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。