locked
How to associate Button state to RoutedCommand without CommandTarget attribute RRS feed

  • Question

  • Hi Geniuses,

    I had a simple xaml file like below,
    <Window x:Class="WindowsApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowsApplication1" Height="300" Width="300"
        >
      <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Cut"/>
      </Window.CommandBindings>
      <DockPanel>
        <ToolBar DockPanel.Dock="Top">
          <Button Command="ApplicationCommands.Cut">
            <Binding RelativeSource="{RelativeSource Self}" Path="Command.Text"/>
          </Button>
        </ToolBar>
        <Grid>
          <TextBox Name="txtBox"/>
        </Grid>
      </DockPanel>
    </Window>

    It works well to disable/enalbe the button which uses RoutedCommand ApplicationCommands.Cut. Someday, I found the toolbar was not necessary and just the button was OK with my app. So I commentted out the <toolbar> element. Then the button can't work any more.

    Since there will be multiple TextBox controls in the window, I don't want to assign a CommandTarget to the CommandBinding. I thiink probably this issue is caused by that the button is a sibling of the TextBox. So it can't get the CanExecuteChanged event when RoutedCommands route through the element tree. But I wonder how ToolBar can solve that.

    Does anyone can help me to solve this problem?

    Thank you in advance.

    Kevin
    Sunday, September 2, 2007 1:39 PM

Answers

  • Surely, Not only Toolbar can solve that, but also Menu. Because WPF's ToolBar and Menu are processed specially to accept current active command object as command target. You can try to set CommandTarget like this:

    <Button Command="Copy" CommandTarget="{Binding ElementName=MyTextBox1}">Copy</Button>.

     

    This article can give you more detail information about this issue.

     

    Sunday, September 2, 2007 3:03 PM
  • Hi Yiling,

     

    Thank for your reference.

     

    The key is the FocusManager.IsFocusScope attached property.

     

    I paste the workable snippet below for others

    <Window x:Class="WindowsApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowsApplication1" Height="300" Width="300"
        >
      <DockPanel>
        <StackPanel DockPanel.Dock="Top" FocusManager.IsFocusScope="True">
          <Button Command="Cut">

            <Binding RelativeSource="{RelativeSource Self}" Path="Command.Text"/>
          </Button>
        </StackPanel>
        <Grid>
          <TextBox Name="txtBox"/>
        </Grid>
      </DockPanel>
    </Window>

    Monday, September 3, 2007 1:06 AM

All replies

  • Surely, Not only Toolbar can solve that, but also Menu. Because WPF's ToolBar and Menu are processed specially to accept current active command object as command target. You can try to set CommandTarget like this:

    <Button Command="Copy" CommandTarget="{Binding ElementName=MyTextBox1}">Copy</Button>.

     

    This article can give you more detail information about this issue.

     

    Sunday, September 2, 2007 3:03 PM
  • Hi Yiling,

     

    Thank for your reference.

     

    The key is the FocusManager.IsFocusScope attached property.

     

    I paste the workable snippet below for others

    <Window x:Class="WindowsApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowsApplication1" Height="300" Width="300"
        >
      <DockPanel>
        <StackPanel DockPanel.Dock="Top" FocusManager.IsFocusScope="True">
          <Button Command="Cut">

            <Binding RelativeSource="{RelativeSource Self}" Path="Command.Text"/>
          </Button>
        </StackPanel>
        <Grid>
          <TextBox Name="txtBox"/>
        </Grid>
      </DockPanel>
    </Window>

    Monday, September 3, 2007 1:06 AM