Wpf Command does not work with Button - net 3.0 vs2008

R

Rolf Welskes

Hello,
I have the following simple xaml:

<Window x:Class="CommandTest01.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="293" Width="490">

<Grid Background="LightGray">
<Menu Height="22" HorizontalAlignment="Left" Margin="34,14,0,0"
Name="menu1x" VerticalAlignment="Top" Width="200" >
<MenuItem Header="Test" >
<MenuItem Command="Copy"/>
<MenuItem Command="Paste"/>
</MenuItem>
</Menu>
<Button Height="23" Command="ApplicationCommands.Copy"
HorizontalAlignment="Left" Margin="255,16,0,0" Name="Copy"
VerticalAlignment="Top" Width="75">Copy</Button>
<Button Height="23" Command="ApplicationCommands.Paste"
Margin="0,16,60,0" Name="Paste" VerticalAlignment="Top"
HorizontalAlignment="Right" Width="72">Paste</Button>
<TextBox Height="23" HorizontalAlignment="Left" Margin="48,99,0,0"
Name="textBox1" VerticalAlignment="Top" Width="120" Text="it's a fine
day"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="49,143,0,0"
Name="textBox2" VerticalAlignment="Top" Width="120" Text=""/>
</Grid>


</Window>

MenuItem and buttons have the commands copy and paste.
2 textboxes.
The following happens:
select in the first textbox a part of the text.
Now look in the menu: the item copy changes from disabled to enabled, all is
fine.
BUT: the button copy, which has the same command does not change from
disabled to enabled.

Remark: textboxes have there own commandindings for copy/paste, so there is
no need to make it self.


This is a general problem.

Thank you for any help.

Best Regards
Rolf Welskes
 
M

miher

Rolf Welskes said:
Hello,
I have the following simple xaml:

<Window x:Class="CommandTest01.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="293" Width="490">

<Grid Background="LightGray">
<Menu Height="22" HorizontalAlignment="Left" Margin="34,14,0,0"
Name="menu1x" VerticalAlignment="Top" Width="200" >
<MenuItem Header="Test" >
<MenuItem Command="Copy"/>
<MenuItem Command="Paste"/>
</MenuItem>
</Menu>
<Button Height="23" Command="ApplicationCommands.Copy"
HorizontalAlignment="Left" Margin="255,16,0,0" Name="Copy"
VerticalAlignment="Top" Width="75">Copy</Button>
<Button Height="23" Command="ApplicationCommands.Paste"
Margin="0,16,60,0" Name="Paste" VerticalAlignment="Top"
HorizontalAlignment="Right" Width="72">Paste</Button>
<TextBox Height="23" HorizontalAlignment="Left" Margin="48,99,0,0"
Name="textBox1" VerticalAlignment="Top" Width="120" Text="it's a fine
day"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="49,143,0,0"
Name="textBox2" VerticalAlignment="Top" Width="120" Text=""/>
</Grid>


</Window>

MenuItem and buttons have the commands copy and paste.
2 textboxes.
The following happens:
select in the first textbox a part of the text.
Now look in the menu: the item copy changes from disabled to enabled, all
is fine.
BUT: the button copy, which has the same command does not change from
disabled to enabled.

Remark: textboxes have there own commandindings for copy/paste, so there
is no need to make it self.


This is a general problem.

Thank you for any help.

Best Regards
Rolf Welskes

Hi,

Set the buttons to be focus scopes :
<Button Command="ApplicationCommands.Copy" FocusManager.IsFocusScope="True"
Name="Copy" >Copy</Button>

Hope You find this useful.
-Zsolt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top