11075

WPF routed command enabling works with menu but not with a button

In the following example the menu is enabled when the text receives the focus but not the button. I have tried it with just the button and the text box but the behaviour is the same.

<Window x:Class="WpfPopup.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"> <DockPanel> <Menu DockPanel.Dock="Top"> <MenuItem Command="ApplicationCommands.Paste" /> </Menu> <TextBox BorderBrush="Black" BorderThickness="2" Margin="25" TextWrapping="Wrap" x:Name="text1" Height="58" Width="203" > The MenuItem will not be enabled until this TextBox gets keyboard focus </TextBox> <Button Content="Button" Height="23" Name="button1" Width="93" Command="ApplicationCommands.Paste" /> </DockPanel>

Answer1:

There are two simple ways to fix this:

1) Use FocusManager.IsFocusScope:

<Button Content="Button" Height="23" Name="button1" Width="93" Command="ApplicationCommands.Paste" FocusManager.IsFocusScope="True"/>

2) Set the CommandTarget on the button manually:

<Button Content="Button" Height="23" Name="button1" Width="93" Command="ApplicationCommands.Paste" CommandTarget="{Binding ElementName=text1}" />

You are probably wondering why this works for the menu item? If you read the documentation for FocusManager.IsFocusScope attached property you will get the answer:

By default, the Window class is a focus scope as are the Menu, ContextMenu, and ToolBar classes. An element which is a focus scope has IsFocusScope set to true.

Very confusing when you don't know that!

Recommend

  • inter related stack panel sizing
  • regarding WPF validation;
  • iOS: how to respond to up/down/left/right arrow event from physical keyboard?
  • KeyBinding woes
  • disable menu bar and dock programmatically in mac
  • Problem with Windows Phone ComboBox Style
  • How can I Display UserControl into a form
  • Line with fill and stroke
  • Why does CollectionViewSource.GetDefaultView(…) return the wrong CurrentItem from inside a Task thre
  • Having an issue with my TextBox control template
  • Powerpoint 2010 VSTO AddIn taskpane on multiple Windows
  • How to clip image to ellipse in XAML
  • Keep transition effect on 1 div from moving the div that follows?
  • richtextbox to string
  • Updating one element of a bound Observable collection
  • How to convert WPF project so it can be used as a class library by a separate exe
  • Android Lock Screen C# .NET Replica
  • Dynamically add UI elements to StackPanel after button pressed
  • WP8.1 AppBarButton holding event
  • Can't delete or rename original file after resizing
  • Bypass multiple inheritance in Java
  • D3 get axis values on zoom event
  • wxPython: displaying multiple widgets in same frame
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deleting and Updating values from a cusrsor adapter
  • How to set/get protobuf's extension field in Go?
  • Window Size for Mac application
  • How to show dropdown in excel using jrxml (jasper api)?
  • To display the title for the current loaction in map in iphone
  • trying to dynamically update Highchart column chart but series undefined
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • python draw pie shapes with colour filled
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?