生活随笔
收集整理的這篇文章主要介紹了
WPF:数据绑定--PropertyChangeNotification属性更改通知
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?
PropertyChangeNotification屬性更改通知
實現(xiàn)效果:
1.拍賣金額自動隨屬性值變化而通知界面綁定的值變化。
關(guān)鍵詞 :
INotifyPropertyChangedObservableCollection<T>界面xaml:
數(shù)據(jù)模板
<DataTemplate x:Key="BidItemDataTemplate"><Canvas Width="300" Height="20"> <TextBlock FontSize="14" Foreground="DarkSlateGray" Width="180" Canvas.Left="0" Text="{Binding Path=BidItemName}"/> <TextBlock FontSize="14" Foreground="DarkSlateBlue" Text="$" Canvas.Left="180"/> <TextBlock FontSize="14" Foreground="DarkSlateBlue" Width="80" Canvas.Left="190" Text="{Binding Path=BidItemPrice}"/> </Canvas> </DataTemplate> <ItemsControl Name="MyListBox" DockPanel.Dock="Top" Background="Silver" Width="315" Height="80" ItemsSource="{Binding Source={StaticResource MyDataSource}}" ItemTemplate="{StaticResource BidItemDataTemplate}"/> 后臺代碼:
數(shù)據(jù)類,顯示其中的一個屬性更通知:
public decimal BidItemPrice
{get { return _biditemprice; }set { if (_biditemprice.Equals(value) == false) { _biditemprice = value; 具體業(yè)務(wù)代碼:
初始化類時執(zhí)行每隔一段時間,更新屬性的值,然后作為綁定源反映到界面數(shù)值變動。
public class BidCollection : ObservableCollection<Bid> { private readonly Bid _item1 = new Bid("Perseus Vase", (decimal) 24.95); private readonly Bid _item2 = new Bid("Hercules Statue", (decimal) 16.05); private readonly Bid _item3 = new Bid("Odysseus Painting", (decimal) 100.0); public BidCollection() { Add(_item1); Add(_item2); Add(_item3); CreateTimer(); } private void Timer1_Elapsed(object sender, ElapsedEventArgs e) { _item1.BidItemPrice += (decimal) 1.25; _item2.BidItemPrice += (decimal) 2.45; _item3.BidItemPrice += (decimal) 10.55; } private void CreateTimer() { var timer1 = new Timer { Enabled = true, Interval = 2000 }; timer1.Elapsed += Timer1_Elapsed; } } 擴展:
設(shè)置 Enabled 到 true 等同于調(diào)用 Start, ,而設(shè)置 Enabled 到 false 等同于調(diào)用 Stop。如果 Enabled 設(shè)置為 true 和 AutoReset 設(shè)置為 false, ,則 Timer 引發(fā) Elapsed 事件僅當(dāng)?shù)谝淮伍g隔到期。要引發(fā)的信號 Elapsed 事件總是在排隊等待執(zhí)行 ThreadPool 線程。 這可能會導(dǎo)致 Elapsed 后引發(fā)事件 Enabled 屬性設(shè)置為 false。??
轉(zhuǎn)載于:https://www.cnblogs.com/Jeely/p/11075991.html
總結(jié)
以上是生活随笔為你收集整理的WPF:数据绑定--PropertyChangeNotification属性更改通知的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。