开源WPF-Samples-netframework学习(1)Data Binding——PropertyChangeNotification

该案例展示了在WPF中如何利用INotifyPropertyChanged接口来实现实时更新界面。当属性值改变时,通过调用OnPropertyChanged方法通知界面刷新。数据绑定示例中,TextBlock控件绑定到BidItemName和BidItemPrice属性,实现了价格和名称的动态显示。
摘要由CSDN通过智能技术生成

案例地址:GitHub - microsoft/WPF-Samples: Repository for WPF related samples

一、运行界面

  二、案例功能描述

通过计时器控制属性值改变,属性值改变通知界面刷新

知识点:

1、INotifyPropertyChanged: 通知客户端属性值已更改。

但是也会受到绑定模式的影响。

三、分析代码

1、实现可通知属性值改变的类,继承INotifyPropertyChanged

public class Bid : INotifyPropertyChanged
    {
        private string _biditemname;
        private decimal _biditemprice;

        public Bid(string newBidItemName, decimal newBidItemPrice)
        {
            _biditemname = newBidItemName;
            _biditemprice = newBidItemPrice;
        }

        public string BidItemName
        {
            get { return _biditemname; }
            set
            {
                if (_biditemname.Equals(value) == false)
                {
                    _biditemname = value;
                    // Call OnPropertyChanged whenever the property is updated
                    OnPropertyChanged("BidItemName");
                }
            }
        }

        public decimal BidItemPrice
        {
            get { return _biditemprice; }
            set
            {
                if (_biditemprice.Equals(value) == false)
                {
                    _biditemprice = value;
                    // Call OnPropertyChanged whenever the property is updated
                    OnPropertyChanged("BidItemPrice");
                }
            }
        }

        // Declare event
        public event PropertyChangedEventHandler PropertyChanged;
        // OnPropertyChanged to update property value in binding
        private void OnPropertyChanged(string propName)
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propName));
        }

    }

2、数据绑定

<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}"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值