Suspend Databinding of Controls

前端 未结 6 1453
终归单人心
终归单人心 2021-02-05 13:00

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 d

6条回答
  •  轮回少年
    2021-02-05 13:08

    To deal with the source set the UpdateSourceTrigger to be Explicit.

    
    

    Then in code behind reference a service which can deal with the actual updating as defined by your conditions.

    BindingExpression be = myTextBox.GetBindingExpression(TextBox.TextProperty);
    be.UpdateSource();
    

    This will allow you to specify at which point the data goes back to the source from the target.

    The target can be addressed by making a call to the same referenced service which has the knowledge on when to call the INotifyPropertyChanged.PropertyChanged event within your ViewModel.

        class Data : INotifyPropertyChanged
        {
            Manager _manager;
    
            public Data(Manager manager)
            {
                _manager = manager;
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            String _info = "Top Secret";
            public String Information
            {
                get { return _info; }
                set 
                {
                    _info = value;
    
                    if (!_manager.Paused)
                    {
                        PropertyChangedEventHandler handler = PropertyChanged;
                        if (handler != null)
                            handler(this, new PropertyChangedEventArgs("Information"));
                    }
                }
            }
        }
    

提交回复
热议问题