Lets say I have next layout:
Window
UserControl
UserControl
UserControl
Button
GridControl
GridCell
And lets say that GridCell currently has Keyboard focus. If a user clicks on button. a message is displayed to user to confirm action. No matter of what choice the user selected (Yes or No), a focus should return to a CurrentCell on GridControl. By default, after a user selected some option, a focus would return to Window (reported by Snoop). I would assume that a Button that was clicked would retain focus, but apparently not.
Handling of button Command is done in ViewModel (MVVM).
How do I return keyboard focus to a current cell in grid?
You can probably pretty safely work around this problem by setting FocusManager.IsFocusScope="true" on your button or if there are multiple buttons the parent element they are in (eg StackPanel or whatever).
There are a few potential issues with this if you are using RoutedCommands. Basically RoutedCommands don't always work the way you expect them to inside of focus scopes. It sounds like you are binding directly to a command on the View Model though so that shouldn't be a problem. If you would like to read more about the RoutedCommand issue check this code project article out.
Here is what my sample code to verify this works looks like for your reference.
XAML:
<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">
<Grid Margin="25">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<!--You could also have the FocusManager.IsFocusScope set on the Border instead-->
<Border Margin="0,0,0,15">
<Button FocusManager.IsFocusScope="True" Click="ButtonBase_OnClick">Click me!</Button>
</Border>
<TextBox Grid.Row="1" x:Name="MessageTextBox"></TextBox>
</Grid>
</Window>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
MessageBox.Show("Clicked, message: " + MessageTextBox.Text);
}
}
Related
I created sample Wpf application and installed Extended WPF Toolkit (NuGet package). Here's my xaml code for showing BusyIndicator.
<Window x:Class="WpfApp3.Progress"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
WindowStyle="None"
BorderThickness="0"
Title=""
VerticalAlignment="Center"
HorizontalAlignment="Center"
SizeToContent="WidthAndHeight"
d:DesignWidth="300"
d:DesignHeight="300">
<xctk:BusyIndicator IsBusy="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"></xctk:BusyIndicator>
</Window>
Showing the progress window is triggered with this code:
private void Button_Click(object sender, RoutedEventArgs e)
{
Progress w = new Progress
{
Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
w.Show();
}
My question is simple. How to show BusyIndicator in the middle of the MainWindow screen. As Shown from the picture below it's not centered as it should be. Note that I use SizeToContent="WidthAndHeight"
Having a seperate window to display the busy indicator and will result in unwanted behaviour. What happens if the original window is maximised, moved etc.?
Consider adding the busy indicator to the main screen. Usually I create an overlay region that is used to display message dialogs, progress bars etc..
<Window>
<Grid>
<Application stuff ....>
</Application stuff>
<ContentControl regions:RegionManager.RegionName="OverlayRegion"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</Grid>
</Window>
I'm using Prism here but you can replace the ContentControl with anything, such as the BusyIndicator and manipulate the visibility of the control.
Solved it by removing Window.SizeToContent property and by adding VerticalAlignment and HorizontalAlignment properties to BusyIndicator (actually now I used spinner, but this doesn't make any difference in solution).
<Window x:Class="Test.Progress"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
WindowStyle="None"
AllowsTransparency="True"
BorderThickness="0"
Title=""
WindowStartupLocation="CenterOwner"
ShowInTaskbar="False"
Background="Transparent"
d:DesignWidth="200"
d:DesignHeight="200"
MaxWidth="250"
MaxHeight="250">
<xctk:BusyIndicator IsBusy="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"></xctk:BusyIndicator>
</Window>
The problem is because BusyIndicator was not designed to use it in separate window
The BusyIndicator is a ContentControl. What this means is that the BusyIndicator can contain a single child element within it?s open and closing tags.
Additionally if you do, first thing that is shown is small ui control (12x12 pixels) and after some latency finally the progress bar/busy indicator is shown.
By specifying SizeToContent='WidthAndHeight', xaml automatically resizes height and width relative to content. In this case (12x12) ui control is taken, and after some latency time finally as mentioned above busy indicator is shown. But by that time, the xaml ui renderer already applied SizeToContent and therefore you would have to manually re-position the progress bar.
I'm not really sure how xaml ui renderes works without specifying SizeToContent, but apparently it re-positions the busy indicator correctly after it is shown.
As soon as I set AllowsTransparency to true in a Window and close a Popup in front of this Window, the Window is not redrawn and the popup still shows up on top of the Window.
If this Window lose focus or If I enter the "alt" key of the keyboard, the Window is redrawn and displays correctly.
To be more clear:
Without AllowTransparency
Popup shown:
Popup hidden:
With AllowTransparency
Popup shown:
Popup hidden:
XAML:
<Window x:Class="Tests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="1000"
WindowStyle="None"
AllowsTransparency="True">
<Grid>
<Rectangle Width="1000" Height="300" Fill="Red"></Rectangle>
<CheckBox x:Name="PopupCheckBox">
<TextBlock Text="Show popup"/>
</CheckBox>
<Popup IsOpen="{Binding ElementName=PopupCheckBox, Path=IsChecked}" Placement="Center">
<Rectangle Width="500" Height="500" Fill="Green" />
</Popup>
</Grid>
</Window>
I would like to know what is the cause of this behavior and how to fix/bypass it.
Thank you.
Edit
I tried to call InvalidateVisual in the handler of the popup's Closed event but it did not change the behavior:
public MainWindow()
{
InitializeComponent();
PopupTest.Closed += PopupTest_Closed;
}
void PopupTest_Closed(object sender, EventArgs e)
{
InvalidateVisual();
}
Edit 2
Ok so it appear that the graphic card driver is the cause of this behavior. I updated it to the latest one and while an artifact is still shown, it behaves differently (shown area of the popup is much smaller).
Closing this thread. Thanks to all.
As stated in the question's latest edit, the artifact is caused by outdated graphic card driver.
I can't tell you the cause but to fix this issue use the method InvalidateVisual on your window when closing the popup.
window.InvalidateVisual();
I have a user control in my wpf application. and inside this user control i have web browser control.
when I navigate to some web page, it is not showing up anything. I can see that http call is happening and navigated event of web browser control is firing. but nothing is showing up.
Any help would be appreciated.
XAML looks like this
<UserControl
x:Class="Client.Module.LmpView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
DataContextChanged="UserControl_DataContextChanged"
xmlns:module="clr-namespace:Client.Module"
xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
<!--
<StackPanel>
<WindowsFormsHost x:Name="wfh" Width="400" Height="400"/>
</StackPanel>
-->
<TextBlock>
<WebBrowser Width="400" Height="400" Navigated="LinkedInProfileDisplayControl_Navigated" x:Name="LinkedInProfileDisplayControl" AllowDrop="True" />
</TextBlock>
</UserControl>
Code behind file
private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (this.DataContext != null)
{
LinkedInProfileDisplayControl.Navigate((this.DataContext as LinkedInMemberProfileViewModel).LinkedInMemberProfileUrl);
}
}
try any of these:
1) make sure you have no AllowsTransparency="true" anywhere
2) see if LinkedInProfileDisplayControl.Navigate("http://google.com") works
3) make sure WebBrowser is actually showing, set its Visibility=Visible.
4) see if it works when you detach from navigated event.
5) Use snoop to inspect visual tree.
I have a RadBusyIndicator on my UserControl like so:
<Grid>
<!-- Other Content -->
<t:RadBusyIndicator IsBusy="{Binding IsBusy}"></t:RadBusyIndicator>
</Grid>
And whenever I click in the design view it goes to the BusyIndicator.
I can set the Panel.ZIndex to be negative to select the "Other Content", but this will cause the RadBusyIndicator to be behind the "Other Content"
I tried using a binding for the ZIndex like so:
<t:RadBusyIndicator Panel.ZIndex="{Binding BusyZIndex}" IsBusy="{Binding IsBusy}"></t:RadBusyIndicator>
But it doesn't help.
So the question is:
How do I have the RadBusyIndicator on "Top" of all the "Other Content" but still be able to click(in the designer) and go to the xaml line for that control?
The BusyIndicator needs to be "on top" to be in front of the controls. That makes it also on top in the designer.
There may be better ways of solving this, but what comes into mind is to make the BusyPanel a Resource on the UserControl, and then add it in the Grid control OnApplyTemplate or Loaded by code.
Here is the UserControl's XAML.
<UserControl x:Class="WpfApplication2.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:t="REFERENCE.TO.THE.RAD.ASSEMBLY"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<t:RadBusyIndicator x:Key="TheBusyIndicator" IsBusy="{Binding IsBusy}">
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Button Content="Some content to the button"
Height="25"
Width="200"/>
</Grid>
</UserControl>
I have added the BusyIndicator as a Resource with the Key "TheBusyIndicator".
I have also added x:Name="LayoutRoot" to the Grid which will contain the BusyIndicator.
The Grid can of course have another name if it in fact is not the layout root control.
By adding the BusyIndicator to the Children collection last, it will appear in front of all other controls that are added by the markup code.
Here is the code
UserControl's Constructor:
public UserControl1()
{
this.Loaded += new RoutedEventHandler(UserControl1_Loaded);
}
The execting code:
private void UserControl1_Loaded(object sender, RoutedEventArgs e)
{
this.LayoutRoot.Children.Add(this.Resources["TheBusyIndicator"] as RadBusyIndicator);
}
I never use UserControls anymore, only CustomControls where the XAML goes to "Generic.xaml" and I have no editor to work in. So I have not seen this problem for a while.
I am writing a GUI application to run on a touchscreen device using VB.NET and WPF--it must be full screen at all times, like a kiosk app; the window must not be able to resize or move in any way. The window contains a ListBox that users can currently scroll through by dragging across the list. The problem I'm seeing is that when the user drags across the list, the whole window moves a bit, exposing the desktop underneath, then springs back into place once the user stops dragging. I have not been able to figure out how to keep the window stationary while still allowing users to drag across the ListBox to view all list items. Here is a somewhat simplified version of my code:
<Window
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
WindowStyle="None"
WindowState="Maximized"
WindowStartupLocation="CenterScreen"
KeyboardNavigation.TabNavigation="None"
Topmost="True"
Focusable="False"
ResizeMode="NoResize"
ShowInTaskbar="False"
MaxHeight="1080px"
MaxWidth="1920px">
<Grid>
<ListBox
x:Name="docList"
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
BorderThickness="0">
<TextBlock Text="Item1" />
<TextBlock Text="Item2" />
<TextBlock Text="Item3" />
<TextBlock Text="Item4" />
<TextBlock Text="Item5" />
<TextBlock Text="Item6" />
</ListBox>
</Grid>
</Window>
I believe that if you handle the OnManipulationBoundaryFeedback(object sender, TouchEventArgs e) event on the listbox, and set the e.Handled property true, that should prevent the "bounce" of the application window.
It may also be possible (I hadn't thought of it until just now) to handle the event at the Window level, since it is a bubbling event, to mitigate the chance of any other controls causing the same behaviour.