似乎有些人在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...
  }

允许用户单击切换按钮并从下拉列表中进行选择,以选择选项或键入新值,例如标准组合框控件。

我的视图有一个绑定到包含Gender属性的视图模型的EditableCombo控件;
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);
  }
}

我的视图模型使用MonitoredNotificationObject来维护撤消/重做历史并通知任何属性更改;
    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-同样,撤消和重做将按预期执行。

问题是用户从下拉列表中选择EditableCombo中的新值时。字段更新,性别设置完毕,一切看起来都很好。单击“撤消”成功地将字段改回其原始值-一切看起来不错。

但是,当用户尝试重做更改时,屏幕上的值不会更新。更改基础值,调用Gender属性的get方法,并正确设置Model.Gender值。但是什么也没有。屏幕不更新。 editablecombo控件TextBoxChangedEvent不会触发,因此屏幕上的值不正确就不足为奇了。

基本上不会将更改通知给控件。

有任何想法吗?

更新:

包含EditableCombo的视图具有包含Gender属性的视图模型。属性是这样绑定的;
<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" />

当通过键盘输入新值时,我的undo / redo实现对非editablecombo控件和editablecombo都适用。仅当通过下拉切换按钮更改了属性时,重做问题才明显。我知道基础值已按照之前的说明正确更新(并且例如,随着ValidatesOnDataErrors的启用,当我重做并将Gender属性重新设置为有效值时,红色边框表示错误消失了-但是,文本保持不变)。

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

最佳答案

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

Model.Gender = value;

给财产设定者?

09-25 17:34