UserControl OnPropertyChanged问题

时间:2011-09-26 12:55:37

标签: wpf user-controls

我的UserControl类中有OnPropertyChanged方法:

private static void OnColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        ColorPicker colorPicker = (ColorPicker)sender;

        Color oldColor = (Color)e.OldValue;
        Color newColor = (Color)e.NewValue;
        colorPicker.Red = newColor.R;
        colorPicker.Green = newColor.G;
        colorPicker.Blue = newColor.B;

        colorPicker._previousColors.Push(oldColor); //don't update if undo command executed
        colorPicker.OnColorChanged(oldColor, newColor);
    }

_previousColors是:

private Stack<Color> _previousColors = new Stack<Color>();

如果执行了undo命令,我不想更新此堆栈:

private void UndoCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = _previousColors.Count > 1;
    }

    private void UndoCommand_Executed(object sender, ExecutedRoutedEventArgs e)
    {
        var color = _previousColors.Pop();
        this.Color = color;

    }

如何在OnColorChanged方法中实现这一条件?

1 个答案:

答案 0 :(得分:1)

好像你可以拥有私有bool(让我们称之为isUndoing),它在undo执行开始时设置为true,并在结束时重置为false。在OnColorChanged中,查看isUndoing的值并采取适当的操作。这会不起作用吗?

另外,我同意Rachel,upvoting和Answer标记是你的朋友。每次你给某人一些信誉好的帖子,人们将来更有可能帮助你,