Reputation: 149
I have copy paste button in my application. Initially both button has to be disabled, If I select any text from any textbox, then only copy button needs to be enabled. And once I copied something, Paste button needs to be enabled. I use this code in App.xaml.cs file:
My question is, how would I know that when do i have to enable this button, How to find out in CanCmdCopy function, if I selected any text or not ?
#region CopyCommand
private void CmdCopy(object sender, ExecutedRoutedEventArgs args)
{
// code///
}
private void CanCmdCopy(object sender, CanExecuteRoutedEventArgs args)
{
args.CanExecute = ?????
}
#endregion
Thanks
Dee
Upvotes: 0
Views: 1199
Reputation: 54532
You should be using the default ApplicationCommands, they will have the behavior you are looking for. If you use a ToolBar
it will work without doing anything else, otherwise you will have to use the FocusManager.IsFocusScope property or Bind the CommandTarget
directly. Beware that if there is anything in the Clipboard the Paste Button will be enabled. You can use the ClipBoard.Clear
Method to reset the Clipboard.
i.e:
Example 1
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel >
<ToolBar>
<Button Command="ApplicationCommands.Copy">Copy</Button>
<Button Command="ApplicationCommands.Paste">Paste</Button>
</ToolBar>
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
</StackPanel >
</Window>
Example 2
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel >
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
<TextBox BorderBrush="Black" BorderThickness="2" Margin="5" TextWrapping="Wrap" />
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" FocusManager.IsFocusScope="True" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Height="50" Command="ApplicationCommands.Copy" >Copy</Button>
<Button Grid.Column="1" Height="50" Command="ApplicationCommands.Paste">Paste</Button>
</Grid >
</StackPanel >
</Window>
Upvotes: 3