WP8的绑定属性变化时通知UI

我们知道在使用ObservableCollection作为LongListSelector的ItemsSource时,当源集合发生新增或删除元素时,会即时通知到UI作出更新。

但是当只是修改源集合里元素的属性值时,却不会通知UI更新。

为了使属性变化能够通知UI,需要为源集合的元素类实现INotifyPropertyChanged接口。

首先我们抽象一个实现INotifyPropertyChanged接口的基类BindableBase:

/// <summary>
    /// Implementation of <see cref="INotifyPropertyChanged"/> to simplify models.
    /// </summary>
    [Windows.Foundation.Metadata.WebHostHidden]
    public abstract class BindableBase : INotifyPropertyChanged
    {
        /// <summary>
        /// Multicast event for property change notifications.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Checks if a property already matches a desired value.  Sets the property and
        /// notifies listeners only when necessary.
        /// </summary>
        /// <typeparam name="T">Type of the property.</typeparam>
        /// <param name="storage">Reference to a property with both getter and setter.</param>
        /// <param name="value">Desired value for the property.</param>
        /// <param name="propertyName">Name of the property used to notify listeners. </param>
        /// <returns>True if the value was changed, false if the existing value matched the
        /// desired value.</returns>
        protected bool SetProperty<T>(ref T storage, T value, String propertyName)
        {
            if (object.Equals(storage, value)) return false;

            storage = value;
            if(null != PropertyChanged)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            return true;
        }
    }

再新建我们的元素类,让它继承自BindableBase:

public class StockItem : BindableBase
	{
		string _stockName = string.Empty;
		string _stockCode = string.Empty;
		string _latest = string.Empty;
		float _zf = 0.0f;
		int _displayType = 0;
		public string StockName
		{
			get
			{
				return _stockName;
			}
			set
			{
				if (null != value)
					this.SetProperty(ref _stockName, value, "StockName");
			}
		}
		public string StockCode
		{
			get
			{
				return _stockCode;
			}
			set
			{
				if (null != value)
					this.SetProperty(ref _stockCode, value, "StockCode");
			}
		}
		public string Latest
		{
			get
			{
				return _latest;
			}
			set
			{
				if (null != value)
					this.SetProperty(ref _latest, value, "Latest");
			}
		}
		public float Zf
		{
			get
			{
				return _zf;
			}
			set
			{
				this.SetProperty(ref _zf, value, "Zf");
			}
		}
		//0 - 普通, 1 - 分时, 2 - K线
		public int DisplayType
		{
			get
			{
				return _displayType;
			}
			set
			{
				this.SetProperty(ref _displayType, value, "DisplayType");
			}
		}
		public string Percent
		{
			get
			{
				if (Math.Abs(Zf) < 0.0001f)
					return "0.00%";
				else
				{
					if (Zf > 0.00f)
						return "+" + Zf.ToString() + "%";
					else
						return "-" + Zf.ToString() + "%";
				}
			}
		}
		public string PercentColor
		{
			get
			{
				if (Math.Abs(Zf) < 0.0001f)
					return "Gray";
				else
				{
					if (Zf > 0.00f)
						return "Red";
					else
						return "Green";
				}
			}
		}
	}

最后,使用ObservableCollection作为该元素集合赋给LongListSelector的ItemsSource,那么在修改元素属性时就能通知更新UI了。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值