Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
367 views
in Technique[技术] by (71.8m points)

user interface - WPF Undo/Redo buttons turn grey

i'm developing a UI with WPF where i want to add buttons for the Undo/Redo inside the application actions. For example, if I input in TextBox1 a value, than another value, by pushing undo it turns back to the first value in textbox1.

I've tried to add a Command="ApplicationCommands.Undo" to the button but it turns grey and it is not clickable anymore on the UI:

<Button x:Name="undoBtn"
             BorderBrush="#FFABADB3"
             Background="{x:Null}"
             Padding="5,0,5,0"
             HorizontalAlignment="Left"
             Width="100"
             Height="25"
             VerticalAlignment="Top"
             Margin="15,130,0,0"
             Click='undoBtn_Click'
             Command="ApplicationCommands.Undo"
             >

enter image description here

What am i doing wrong? Thank you very much.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the documentation for the ApplicationCommands class:

The commands in the ApplicationCommands class [...] only represent the instance of the RoutedCommand and not the implementation logic for the command. The implementation logic is bound to the command with a CommandBinding.

[... However, m]any controls do provide implementation logic for many of the commands in the command library. For example, the TextBox class provides logic for the Paste, Cut, Copy, Undo, and Redo commands.

To utilize the implementation for the Undo/Redo commands provided by the TextBox class, set the target of the command to the TextBox element.

<TextBox Name="TextBox1"/>
<Button Content="Undo" 
        Command="ApplicationCommands.Undo" 
        CommandTarget={Binding ElementName=TextBox1}"/>

To answer your question, the Button is not enabled because the CanExecute property of the CanExecuteRoutedEventArgs for your CommandBinding's CanExecuteRoutedEventHandler is not set to true because your command does not have a target.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...