暂停控件的数据绑定 [英] Suspend Databinding of Controls

查看:101
本文介绍了暂停控件的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的数据绑定到该改变每一秒左右的值控制。不时,我需要暂停的控制,以使它们不更新其数据绑定(在任一方向上)。我后来需要取消暂停的控制,使他们能够与他们的价值观更新数据源,并接收从源头正常将来的更新。我如何做到这一点?

样品绑定:

 <文本框文本={结合UpdateSourceTrigger =引发LostFocus,模式=双向,路径=的myData}>
 

解决方案

您不必中止绑定。另外,可能还有更简单的,这样做,这是暂停在视图模型更改通知。例如:

 私人的HashSet<字符串> _ChangedProperties =新的HashSet<字符串>();

私人无效OnPropertyChanged(字符串propertyName的)
{
   如果(_Suspended)
   {
      _ChangedProperties.Add(propertyName的);
   }
   其他
   {
      PropertyChangedEventHandler H =的PropertyChanged;
      如果(H!= NULL)
      {
         H(这一点,新PropertyChangedEventArgs(propertyName的));
      }
   }
}

私人布尔_Suspended;

公共BOOL暂停
{
   {返回_Suspended; }
   组
   {
      如果(_Suspended ==值)
      {
         返回;
      }
      _Suspended =价值;
      如果(!_Suspended)
      {
         的foreach(在_ChangedProperties串propertyName的)
         {
            OnPropertyChanged(propertyName的);
         }
         _ChangedProperties.Clear();
      }
   }
}
 

这将(如果它的调试和测试,这是我没有这样做)停止加的PropertyChanged 事件时,暂停设置为,当暂停设置为再次将引发该事件的每一个变化,而这是悬浮性。

这不会从更新视图模型停止更改绑定的控件。我向你,如果你是在同一时间,你改变它们在后台让屏幕上的用户编辑属性,还有你需要仔细看看的东西,并且它不具有约束力。

I have a series of controls that are databound to values that change every second or so. From time to time, I need to "pause" the controls, so that they do not update their databindings (in either direction). I then later need to "unpause" the controls, so that they can update the datasource with their values, and receive future updates from the source as normal. How do I accomplish this?

Sample Binding:

<TextBox Text="{Binding UpdateSourceTrigger=LostFocus, Mode=TwoWay, Path=myData}">

解决方案

You don't necessarily have to suspend binding. Another, and possibly simpler, way to do this is to suspend change notification in the view model. For instance:

private HashSet<string> _ChangedProperties = new HashSet<string>();

private void OnPropertyChanged(string propertyName)
{
   if (_Suspended)
   {
      _ChangedProperties.Add(propertyName);
   }
   else
   {
      PropertyChangedEventHandler h = PropertyChanged;
      if (h != null)
      {
         h(this, new PropertyChangedEventArgs(propertyName));
      }
   }
}

private bool _Suspended;

public bool Suspended
{
   get { return _Suspended; }
   set
   {
      if (_Suspended == value)
      {
         return;
      }
      _Suspended = value;
      if (!_Suspended)
      {
         foreach (string propertyName in _ChangedProperties)
         {
            OnPropertyChanged(propertyName);
         }
         _ChangedProperties.Clear();
      }
   }
}

This will (if it's debugged and tested, which I haven't done) stop raising PropertyChanged events when Suspended is set to true, and when Suspended is set to false again it will raise the event for every property that changed while it was suspended.

This won't stop changes to bound controls from updating the view model. I submit to you that if you're letting the user edit properties on the screen at the same time that you're changing them in the background, there's something you need to take a closer look at, and it's not binding.

这篇关于暂停控件的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆