Show focused element on loaded control in WPF - wpf

How do I highlight the focused element in my view after it has been loaded? I know it's focused because if I press Enter or navigate with the keyboard it behaves as expected, and if if I navigate back to the first element it gets correctly highlighted. I'd just like to make it clear to the user which element is selected after the view has been loaded. This happens with either buttons or checkboxes.

The focussed style will be applied if the control is the focussed element. A focused element is different to a default element which may allow an action to be performed if enter is pressed.
Setting focus needs to be done explicitly, in the example it is done be the following line
FocusManager.FocusedElement="{Binding ElementName=Button}"
This sets the focus on the button, running this you should see the button is highlighted. Remove the above line and the button will not be highlighted.
<Window x:Class="WpfApp4.MainWindow"
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"
FocusManager.FocusedElement="{Binding ElementName=Button}"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Button IsDefault="False"
Width="150"
Height="30"
Name="Button"
Content="Button"/>
</StackPanel>
</Window>

wow I totally forgot about asking about this. At the end I had to remove the FocusVisualStyle property set on my style and write a simple trigger on the IsFocused event.That took care of my issue.

Related

WPF cannot dock control in a dock panel, canvas seems to instead get centered

When using the xaml markup
<Window x:Class="Foo.Bar.MainWindow"
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"
mc:Ignorable="d"
Title="MainWindow" Height="400" Width="400">
<DockPanel LastChildFill="True">
<Canvas DockPanel.Dock="Right" Width="100" />
</DockPanel>
</Window>
...it seems like the canvas does not get docked to the right.
Why?
Instead it becomes centered.
You have LastChildFill set to true. As your DockPanel only has one child, it is also the last child, so it is being overridden to Fill. The reason it is getting centered is that the Width has been explicitly set to 100. Since it can't fill with that override, it does the best it can, and centers it.
Simply removing LastChildFill will not fix the issue, as the default value is true. To fix this, you will have to explicitly set LastChildFill to false.
The reason for these default settings is that DockPanel is usually used to house multiple child controls, where the children docked to different sides are listed first, and the final child takes up the remaining area. Using it with only a single child is fairly unusual.

The Property 'Click' was not found in Type 'GroupBox'. in netcore 3.1

I have a netcore 3.1 application and would like to add a click event in order to expand / contract the content of the GroupBox.
Unfortunately, Visual Studio throws the error from the title which is stating that GroupBox does not have such an event, even though it is stated so in the Microsoft reference.
Code to reproduce:
<UserControl x:Class="SoundStudio.Views.LibraryView.Library"
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:local="clr-namespace:SoundStudio.Views.LibraryView" -->
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<StackPanel>
<Label Content="Library"></Label>
<Button x:Name="ImportButton" Content="Import" Click="ImportButton_Click"></Button>
<GroupBox Header="Filter V" x:Name="FilterGroupBox" Click="FilterGroupBox_Click">
<!-- <local:Filterview></local:Filterview> -->
</GroupBox>
</StackPanel>
</UserControl>
The documentation you have posted is for Windows Forms, the one for WPF is this. The WPF GroupBox does not have a Click event, but you can use all of the Mouse* events instead like:
Defined on UIElement:
MouseLeftButtonDown
PreviewMouseLeftButtonDown
MouseLeftButtonUp
PreviewMouseLeftButtonUp
...
Defined on Control
MouseDoubleClick
PreviewMouseDoubleClick
<GroupBox Header="Filter V" x:Name="FilterGroupBox" MouseLeftButtonDown="FilterGroupBox_Click">
<!-- <local:Filterview></local:Filterview> -->
</GroupBox>
From another perspective, you might be using the wrong control in the first place.
[...] add a click event in order to expand / contract the content of the GroupBox.
In this case, you could use an Expander, which is exactly designed for this purpose.
Expander
Expander Overview

wpf crystalreport viewer dissappear for no reason

Well the problem is easy to explain and hard to figure out, at least by me, so i have a tabcontrol with 3 tabitem, on one of the tabitems i have a wpf reportviewer that loads a report of some data, when i change tabs, it does not matter to each one when i get back to the tab with the crystalreport in it, the crystalreport viewer is not there anytmore! i also have a button on top to refresh the report and the button is still there but the crystal report control is not there anymore!
my xaml looks like this
<UserControl x:Class="uccvrFactuur"
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:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Content="Report" Height="50" HorizontalAlignment="Right" Margin="5" Name="Button1" VerticalAlignment="Top" Width="50" />
<my:CrystalReportsViewer Name="crvFactuur" Margin="0,60,0,0" />
</Grid>
</UserControl>
i must say the first time i click on that tab the control is there but i change tabs and go back to it the control is not there anymore.
Thanks in advance

WindowsFormsHost causing System.Windows.Forms.Button.Click event to not fire

I'm converting a windows forms application into WPF, and am re-using some of the winform controls for the first part of the conversion.
To host the winform controls, I'm using WindowsFormsHost, and what appears to be happening is that the enter keypress is not causing Button.Click event to be fired on winforms buttons (however, clicking with the mouse and spacebar still works).
You can reproduce this behavior by doing the following:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="350" Width="525">
<Grid>
<WindowsFormsHost>
<wf:Button Text="Hello" Click="Button_Click_1" />
</WindowsFormsHost>
</Grid>
I was thinking of adding an event handler to the WindowsFormsHost to call Button.PerformClick if enter was pressed and a button was focused, but I feel like there must be something I'm overlooking with why enter isn't causing the button click to occur.

WPF XAML Web Browser in Tab Item issue

I am developing an application that involves a web browser object in a tab item of a tab control.
example:
<Window x:Class="TabControl.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>
<TabControl
x:Name="tabControl1">
<TabItem>
<TextBox>Hello</TextBox>
</TabItem>
<TabItem>
<WebBrowser Source="http://www.google.com"></WebBrowser>
</TabItem>
</TabControl>
</Grid>
So, the first time you click on the web browser tab, the focus goes to the search box in google, when you try to go back to the first tab, it requires two clicks, one i assume to take the focus away from the webpage, and another to move the selected tab item? Can anyone offer a suggestion that would enable the tab to be changed with only one click? Cheers!
Add a PreviewMouseLeftButtonDown handler for tabControl1 and set tabControl1 to focus.

Resources