【问题标题】:UWP TextBox method for undo command用于撤消命令的 UWP TextBox 方法
【发布时间】:2017-12-17 17:48:44
【问题描述】:

我知道文本框默认有撤消命令,当我按 ctrl+Z 时。我正在创建 UWP 应用程序,我需要通过按 GUI 中的某个按钮来调用此命令。我该怎么做?有没有类似 textbox.Undo() 的方法?

【问题讨论】:

  • UWP 中有一个用于创建虚假输入数据的 API。不幸的是不记得它的名字,但使用它你应该能够模拟 CTRL+Z 按键。

标签: c# xaml textbox uwp undo


【解决方案1】:

我认为您不会找到像 Textbox.Undo() 这样的东西,但您可以将所有更改保存在一个列表中,并在需要时恢复文本。

.XAML:

<TextBox Text="{Binding MyText}" />

.CS:

private List<string> _history;

private double _myText;
public double MyText
{
    get { return _myText; }
    set
    {
        if(_myText != value) {
            _myText = value;
            _history.Add(value);
            //Notify
        }
    }
}

private void Undo() {
    _myText = _history.LastOrDefault();
    //Notify
}

【讨论】:

  • 我知道这是办法,如何解决。但我想调用默认的撤消命令。我希望应该有某种方式,如何调用它,否则它是无用的,当我们无法随时调用它时。
【解决方案2】:

您可以使用 ApplicationCommand 类。 只需在您想要点击并执行撤消操作的按钮命令中设置ApplicationCommand.Undo..并设置Target type=教科书名称。

    <Button Command="ApplicationCommands.Undo" Width="75" TargetType="{x:Type TextBox}"/>

  <TextBox BorderBrush="Black" BorderThickness="2" Margin="25"
           TextWrapping="Wrap">  
  </TextBox>

This is a link for ApplicationCommand Class

ApplicationCommands Class

【讨论】:

  • 此方案不适用于 UWP
猜你喜欢
  • 2015-10-31
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2014-01-04
  • 2012-01-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多