Silverlight AutoCompleteBox NotifyPropertyChanged 不会更新屏幕



似乎有些人在Silverlight中更新文本AutoCompleteBox时遇到了问题,遗憾的是我已经加入了这个行列。

我有一个名为 EditableCombo 的派生类,如下所示;

  public class EditableCombo : AutoCompleteBox
  {
    ToggleButton _toggle;
    Path _btnPath;
    TextBox _textBox;
    ...animation and toggle button stuff...
    public override void OnApplyTemplate()
    {
      ...animation and toggle button stuff...
      //required to overcome issue in AutoCompleteBox that prevents the text being updated in some instances
      //http://stackoverflow.com/questions/8488968/silverlight-5-autocompletebox-bug?rq=1
      _textBox = GetTemplateChild("Text") as TextBox;
      if (_textBox == null)
        throw new NullReferenceException();
      if (_textBox != null)
        _textBox.TextChanged += TextBoxChanged;
      base.OnApplyTemplate();      
    }
    void TextBoxChanged(object sender, TextChangedEventArgs e)
    {
      Debug.WriteLine("text box changed fired new value: " + _textBox.Text);
      Text = _textBox.Text;
      OnTextChanged(new RoutedEventArgs());      
    }
    ...animation and toggle button stuff...
  }

这使用户能够单击切换按钮并从下拉列表中选择选项或键入新值,如标准组合框控件。

我的视图有一个绑定到包含性别属性的视图模型的可编辑组合控件;

public string Gender
{
  get
  {
    Debug.WriteLine("Gender get executed - Model.Gender = " + Model.Gender);
    return Model.Gender;
  }
  set
  {        
    if (Model.Gender == value) return;        
    MonitoredNotificationObject.RaisePropertyChanged(() => Model.Gender, value, this, true);        
  }
}

我的视图模型使用受监视的通知对象来维护撤消/重做历史记录并通知任何属性更改;

    public void RaisePropertyChanged(Expression<Func<string>> propertyExpression,
                                     string newValue,
                                     object sender,
                                     bool canChain)
    {
      PropertyExpressionHelper propertyExpressionHelper = new PropertyExpressionHelper(propertyExpression)
                                       {
                                         NewValue = newValue
                                       };
#if DEBUG
      VerifyPropertyExists(propertyExpressionHelper.Name, sender);
#endif
      var monitoredAction = new MonitoredProperty<TViewModel, TModel>(this)
                              {
                                ObjectPropertyName = propertyExpressionHelper.MakeObjectPropertyName(),
                                PropertyExpressionHelper = propertyExpressionHelper,
                                Sender = (TViewModel) sender,
                                CanChain = canChain
                              };
      propertyExpressionHelper.SetToNewValue();
      RaisePropertyChanged(propertyExpressionHelper.Name, sender);
      MaintainMonitorState(monitoredAction);
    }
撤消

和重做实现如下(显示撤消);

public override bool UndoExecute(MonitoredObject<TViewModel, TModel> undoAction,
                                 Stack<MonitoredObject<TViewModel, TModel>> redoActions,
                                 Stack<MonitoredObject<TViewModel, TModel>> undoActions)
{
  PropertyExpressionHelper.SetToNewValue();
  redoActions.Push(undoAction);
  var action = (MonitoredProperty<TViewModel, TModel>) undoAction;
  HandleAutoInvokedProperties(action);
  if (action.CanChain)
  {
    if (undoActions.Any())
    {          
      if (CanDoChain(undoActions, action))
        return true;
    }
  }
  action.RaiseChange();
  Sender.RaiseCanExecuteChanges();
  return false;
}

属性更改通知是这样引发的;

protected virtual void RaiseChange()
{
  MonitoredNotificationObject.RaisePropertyChanged(PropertyExpressionHelper.Name, Sender);
  if (RaiseChangeAction != null)
    RaiseChangeAction.Invoke();
}

使用上述方法适用于普通文本框,并成功地允许用户根据需要撤消和重做更改。当用户在字段中键入条目时,这也适用于 EditableCombo - 同样,撤消和重做按预期执行。

问题是当用户从下拉列表中选择"可编辑组合"中的新值时。字段更新,性别设置,一切看起来都很好。单击"撤消"成功将字段更改回其原始值 - 所有看起来都很花花公子。

但是,当用户尝试重做更改时,屏幕上的值不会更新。更改基础值,调用 Get on Gender 属性,并正确设置 Model.Gender 值。但后来什么都没有。屏幕不更新。可编辑组合控件 TextBoxChangedEvent 不会触发,因此毫不奇怪,屏幕上的值不正确。

基本上,控件不会收到更改通知。

有什么想法吗?

更新:

包含可编辑组合的视图具有包含性别属性的视图模型。财产是这样绑定的;

<EditCombo:EditableCombo ItemsSource="{Binding Genders}"
    ItemTemplate="{StaticResource editableComboDataTemplate}"
    Style="{StaticResource EditableComboStyle}"
    Text="{Binding Path=Gender,
                  UpdateSourceTrigger=PropertyChanged,
                  Mode=TwoWay,
                  ValidatesOnDataErrors=True}"
    TextBoxStyle="{StaticResource editableComboDataEntryField}"
    ValueMemberPath="Value" />

我的撤消/重做实现适用于不可编辑组合控件和通过键盘输入新值时的可编辑组合。仅当通过下拉切换按钮更改属性时,重做问题才明显。我知道基础值已正确更新,如前所述(而且,例如,当 ValidatesOnDataErrors 打开时,当我重做并将 Gender 属性设置回有效值时,表示错误的红色边框消失 - 但是,文本保持不变)。

无论出于何种原因,在上述情况下,TextBoxChanged 事件永远不会触发。可能是该事件正在其他地方处理吗?

如果添加此行,它是否有效:

Model.Gender = value;

给财产设定者?

最新更新