How to hide windows or open it behind main aplication - wpf

I need to open new window behind main screen. This code doesn't work for me.
Lets assume I have 2 classes
MainWindow.xaml
<Grid>
<Button Click="ButtonBase_OnClick"></Button>
</Grid>
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Test t=new Test();
t.Show();
}
Test class that I need to open behind Main window
<Window x:Class="WpfApplication1.Test"
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:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="Test" Height="350" Width="525" ShowInTaskbar="False" Visibility="Hidden" >
<Grid>
<TextBox/>
</Grid>
I try to use ShowInTaskbar="False" Visibility="Hidden" but it still doesn't work. The problem I can see "Test" windows 1 sec and after it will be unviable(because Visibility="Hidden") .I need to open this windows many times, it will blinks many times. The better solution to open it behind application.
Is anyone knows how to do?

Try to set the Topmost property of the MainWindow to true:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Test t = new Test();
t.Show();
Topmost = true;
}
This should cause the Test window to be displayed "behind" the main window.

The final solution for me
if (Application.Current.MainWindow != null)
{
Application.Current.MainWindow.Topmost=true;
}

Related

C# WPF XAML Mouse(Down) events not working anymore as of .NET 5(C#9)

Screen shot of the issue I'm running into after going to .NET 5 (C# 9)
I'm trying to figure out how they want us to implement this simple code now.
It says its an error but it still builds the project. Although, this does prevents designer from building the view from xaml, so unable to edit the xaml properly, also prevents the functionality, in this case trying to drag a window with mouse_down event.
XAML:
<Window x:Class="WPFProject.Window1"
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:local="clr-namespace:WPFProject"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<Border MouseDown="CodeBehind_DragFunction">
<TextBlock Text="Hello World" Foreground="Black"/>
</Border>
</Window>
C# Code-behind
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
void CodeBehind_DragFunction(object sender_, MouseButtonEventArgs e_)
{
DragMove();
}
}
This worked in previous versions..
Suggested answer posted by #Pharaz Fadaei
It was an IDE bug that was fixed by following these steps outlined in this post: The event 'foo' is not a RoutedEvent

How to keep window always on desktop

I have made a clock app, when I click ShowDesktop on taskbar it gets hidden. I want to show Window always on desktop, please, help me!
I do not know to much English!
When you click Show Desktop on the start menu, Windows asks all windows to minimize.
Hook into the StateChanged event and override this action with a default setting.
<Window x:Class="WpfApp7.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"
xmlns:local="clr-namespace:WpfApp7"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Topmost="True" StateChanged="MainWindow_OnStateChanged">
</Window>
private void MainWindow_OnStateChanged(object sender, EventArgs e)
{
var window = (Window) sender;
if (window.WindowState == WindowState.Minimized)
window.WindowState = WindowState.Normal;
}
<Window... TopMost="True">
If you set TopMost propety to true, your app will be always on top. You can set it to false when you don't want to keep it on top.

AutomationProperties.LiveSetting not working in WPF in .NET Framework 4.7.1

I have a TextBlock and I want to track that control from Screen reader and whenever a new value is set to the control in code, the screen reader should readout the new text. This is available in WPF from .NET framework 4.7.1 which is mentioned in the MSDN LINK.
But I am always getting null for the AutomationPeer value. What am I missing in the code? Am I doing it in the right way? Please help.
XMAL
<Window x:Class="WPFAccessibility.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"
xmlns:local="clr-namespace:WPFAccessibility"
mc:Ignorable="d"
Title="WPFAccessibility" Height="450" Width="800">
<Grid>
<TextBlock Name="MyTextBlock" AutomationProperties.LiveSetting="Assertive">My initial text</TextBlock>
<Button Name="Save" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="50,321,0,0" Height="49" Click="Save_Click"/>
</Grid>
</Window>
Code
private void Save_Click(object sender, RoutedEventArgs e)
{
// Setting the MyTextBlock text to some other value and screen
// reader should notify to the user
MyTextBlock.Text = "My changed text";
var peer = UIElementAutomationPeer.FromElement(MyTextBlock);
// I am always getting peer value null
peer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
}
Use the CreatePeerForElement method to create a UIElementAutomationPeer for the TextBlock:
private void Save_Click(object sender, RoutedEventArgs e)
{
MyTextBlock.Text = "My changed text";
var peer = UIElementAutomationPeer.FromElement(MyTextBlock);
if (peer == null)
peer = UIElementAutomationPeer.CreatePeerForElement(MyTextBlock);
peer.RaiseAutomationEvent(AutomationEvents.LiveRegionChanged);
}

How to Use RadWindow as MainWindow of WPF application

I want to use RadWindow as the main window of my WPF application. So that outer window also take the theme of overall application. I used this approach, but after that application is not being shown on Taskbar anymore. After reading different threads I came to know that because RadWindow is not child of Window so that's why its not possible.
So What is now I am trying to do, is to hide the outer Window completely somehow and to use RadWindow as a child, but parent of all other controls. Following is XAML
<Window x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None"
Title="MainWindow" >
<telerik:RadWindow WindowStartupLocation="CenterScreen" Width="Auto" Height="Auto" x:Name="MyRadWindow">
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
</Window>
But I am unable to completely hide the outer window. It still shows the borders. Then as a second step I have to handle the minimize, maximize and window close events from this RadWidow.
If anyone have tried this approach please help me out or suggest what would be the better way of doing this?
The main purpose of doing all this is to Sync the GUI of Outerwindow with the current TelerikTheme.
There's a simpler solution using the RadWindowInteropHelper like this:
using Telerik.Windows.Controls.Navigation;
public partial class MyRadWindow : RadWindow
{
public MyRadWindow()
{
InitializeComponent();
RadWindowInteropHelper.SetShowInTaskbar(this, true);
}
}
I think you should try to set main class as telerik window instead of nesting inside normal window:
<telerik:RadWindow x:Class="MyTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
WindowStyle="None" WindowStartupLocation="CenterScreen" ShowInTaskbar="True"
Title="MainWindow" >
<Grid>
<!-- All Controls here -->
</Grid>
</telerik:RadWindow>
Don't forget to change base class of MyTest.MainWindow to RadWindow
EDIT: Sorry didn't notice provided link.
You can try hide main window with overriding it style by setting following attributes:
WindowStyle="None" Background="Transparent" AllowsTransparency ="True"
First open the MainWindow.xaml file and replace the Window declaration with RadWindow declaration:
<telerik:RadWindow x:Class="RadWindowAsMainWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
Loaded="RadWindow_Loaded_1"
Header="MainWindow" Height="350" Width="525">
...
</telerik:RadWindow>
and in code-behind:
`
public partial class MainWindow : RadWindow
{
...
}
2. Then override OnStartup method of the Application class to show the RadWindow:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
new MainWindow().Show();
base.OnStartup(e);
}
}
`
Then in RadWindowLoad event write this:
`
private void RadWindow_Loaded_1(object sender, RoutedEventArgs e)
{
var window = this.ParentOfType();
window.ShowInTaskbar = true;
}
`

focussing cursor in textbox using xaml in wpf

private void TextBox1_TextChanged(object sender, TextChangedEventArgs e)
{
txt1_focus.Focus();
}
How can i achieve the above code using using xaml file in wpf.
use the FocusManager.FocusedElementlike below:
<Window x:Class="UI.Views.MyView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FocusManager.FocusedElement="{Binding ElementName=txtSearch}">
</Window>
http://cloudstore.blogspot.com/2008/06/setting-initial-focus-in-wpf.html
This site explains how to set the initial focus on a certain control.
<Window ...
FocusManager.FocusedElement="{Binding ElementName=TextBox1}">

Resources