[Version 2.0] Focus and buttons

Dec 30, 2012 at 5:18 PM

Using the lastest version(i downloaded the sourcecode) and running this sample shows a problem with buttons and focus. It seems like the first click is being ignored! Just perform the steps in 1, 2, 3 and when you click on 3 the first time nothing happens. It works if you click again, why?

<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="700"
		xmlns:avalonDock="http://avalondock.codeplex.com">
	<Grid>
		<avalonDock:DockingManager>
			<avalonDock:DockingManager.Theme>
				<avalonDock:AeroTheme/>
			</avalonDock:DockingManager.Theme>
			<avalonDock:LayoutRoot>
				<avalonDock:LayoutPanel DockHeight="Auto">
					<avalonDock:LayoutAnchorablePane>
						<avalonDock:LayoutAnchorable Title="button">
							<ScrollViewer>
								<StackPanel VerticalAlignment="Stretch"  >
									<Button Content="3. Click me last, i should show a messagebox on first click" Click="Button_Click">
										<x:Code>
											<![CDATA[
											private void Button_Click(object sender, RoutedEventArgs e)
											{
												MessageBox.Show("click");
											}
											]]>
										</x:Code>
									</Button>
									<TextBlock Background="White" Padding="2" VerticalAlignment="Stretch" Height="196" Width="401" >1. click me</TextBlock>
								</StackPanel>
							</ScrollViewer>
						</avalonDock:LayoutAnchorable>
					</avalonDock:LayoutAnchorablePane>
					<avalonDock:LayoutPanel Orientation="Horizontal">
						<avalonDock:LayoutAnchorablePane>
							<avalonDock:LayoutAnchorable Title="no button">
								<ScrollViewer>
									<TextBlock Background="White" Padding="2" >2. click me, and click the button after</TextBlock>
								</ScrollViewer>
							</avalonDock:LayoutAnchorable>
						</avalonDock:LayoutAnchorablePane>
					</avalonDock:LayoutPanel>
				</avalonDock:LayoutPanel>
			</avalonDock:LayoutRoot>
		</avalonDock:DockingManager>
	</Grid>
</Window>

Jan 15, 2013 at 8:15 PM

Hi Zoot,

I haven't managed to resolve this one either - did you figure out how to solve the problem? 

Jan 18, 2013 at 2:20 PM

Hi,

having the same problem. Is there any solution for this focus problem?

Jan 20, 2013 at 12:49 PM

Hi all,

No i didn't find a real solution..

Jan 21, 2013 at 4:31 PM

Hi all,

after some time of investigation I found the problem. It is caused inside of the FocusElementManager class in Method SetFocusOnLastElement. If I comment out line number 158 the focus problem don't appear anymore. It seems that it only appears if you have some focusable elements in your pane like textboxes or scrollviewers. 

if (_modelFocusedElement.GetValue(model, out objectToFocus))
{
     //focused = objectToFocus == Keyboard.Focus(objectToFocus);
}

I know it's not a real fix. Just a workaround. Maybe the developer can have a deeper look into this issue and release a real patch for this problem.

Jan 22, 2013 at 3:02 AM

Hi all,

I also had some issues with this where in my app a tree view in one of the anchorable keeps documents as nodes. When active document changed from the tabs the selected node in tree view changes. But again it switch back to earlier document. This happens because tree view try to select last focused element in this particular code.

I was able to fix the issue by changing the dispatcher priority to Normal at DockingManager.OnLayoutChanged() line 193 where FocusElementManager.SetFocusOnLastElement(Layout.ActiveContent) is called.

I think the real issue is not using FocusManager.IsFocusScope. Please check following. http://www.codeproject.com/Articles/38507/Using-the-WPF-FocusScope


Jan 24, 2013 at 11:06 AM

I'm pretty sure this zebra here is right!

Toolbars and menus have an own focus scope, and they are giving me the weirdest behavior. 

Coordinator
May 22, 2013 at 2:51 PM
This has been fixed and check in.

The problem I found was in FocusElementManager.manager_PreviewGotKeyboardFocus(...) where the last focused element wasn't considered if it was implementing ICommandSource.

I don't know why adospace added this condition, maybe to avoid to have a MenuItem to hold the last focused item, but since this issue seems to be the top priority for the community (most voted) I've removed this breaking line of code.
Jun 3, 2013 at 4:24 PM
Unfortunately the problem is still not solved. It works for the above example, but it can be easily reproduced in the "AvalonDock.TestApp".
  • Start the "TestApp"
  • In the left tool window "WinForms Window" click in the first/upper textbox. The cursor is now in the textbox
  • Click in the textbox "Document 1 Content" in "Document 1". You already see that the focus immediately changes back to the left tool window
  • Click the button "Click to add 2 documents". Button looks like it is being clicked, but nothing happens. Focus stays in the left tool window
Aug 16, 2013 at 8:40 PM
We're also seeing this issue with the most recent release available via NuGet. Would be interested in knowing if there is another solution pending / available.