How to Use RadWindow as MainWindow of WPF application - wpf

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;
}
`

Related

How can I set region Manager for my dialog window in Prism?

I write my app without using Shell. So I created my own Window using IDialogService and opened in one of my modules.
As far, as I am concerned, Region Manager is attached to Shell, but due to the fact I don't have it, region manager doesn't work when I try to navigate from one view to another.
I know that Region Navigation works fine with the shell (I tested it) and the same code stops working when I substitute the shell with IDialogService.
Here is what I have
<Window x:Class="TechDocs.Views.MainSettingsWindowView"
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:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainSettingsWindow" Height="400" Width="750">
<Grid>
</Grid>
</Window>
Content for the first region. When I click the button, it should navigate to the second region.
<UserControl x:Class="TechDocs.Views.SettingsView"
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:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Command="{Binding NodeSelectedCommand}" Name="Button"/>
<ContentControl prism:RegionManager.RegionName="region"/>
</Grid>
</UserControl>
In module I connect my root window with UserControl which holds the button and Content control for the second region.
public class SettingsModule : IModule
{
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
var dialogService = _containerProvider.Resolve<IDialogService>();
containerRegistry.RegisterDialog<MainSettingsWindow>("MyWindow");
containerRegistry.RegisterDialog<SettingsView>("customView");
containerRegistry.RegisterForNavigation<MyView>();
dialogService.Show("customView");
}
}
And when I click the button I get in this code
public void SelectedNode()
{
regionManager.RequestNavigate("region", "MyView");
}
RequestNavigate doesn't give any exceptions, but still nothing appears on the screen.
Could you please explain how I should register region manager with my window?
Try to initialize the region manager explicitly in your custom window using the static methods below:
RegionManager.SetRegionName(cc, "region");
RegionManager.SetRegionManager(cc, regionManager);
XAML:
<ContentControl x:Name="cc" prism:RegionManager.RegionName="region"/>

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 hide windows or open it behind main aplication

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;
}

How to add UserControl to a Panel on a WPF Window

I think I'm missing something that should be obvious here, but I'm drawing a blank on this one.
I've built a very primitive UserControl containing nothing more than a TextBox to use as a log window:
<UserControl x:Class="My.LoggerControl"
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"
x:Name="LoggerView">
<Grid x:Name="LayoutRoot">
<TextBox x:Name="LogWindow" AcceptsReturn="True"/>
</Grid>
</UserControl>
I don't expect that to be the best way to do it, but it should be good enough for a prototype.
The code-behind is similarly simple:
public partial class LoggerControl : UserControl, ILogger
{
public LoggerControl()
{
InitializeComponent();
}
private LogLevel level = LogLevel.Warning;
#region ILogger
public LogLevel Level
{
get { return level; }
set { level = value; }
}
public void OnError(string s)
{
if (level >= LogLevel.Error)
LogWindow.AppendText("ERROR:::" + s + "\n");
}
// ...
#endregion
}
The thing I can't figure out is how to add this control to my MainWindow.xaml. Simplifying, lets say my window looks like this:
<Window x:Class="My.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My"
Title="Test" Height="350" Width="525">
<Grid>
<local:LoggerControl x:Name="LogView" />
</Grid>
</Window>
Even with something so simple, the Designer in Visual Studio 2010 can't load the main window. The error given is:
A value of type 'LoggerControl' cannot be added to a collectionor dictionary of type 'UIElementCollection'.
This error message has only one unrelated hit in the major search engines (plus duplicates) so I haven't found any useful help. Microsoft's own documentation seems to imply that this should work.
Any idea how to solve this?
<UserControl x:Class="My.LoggerControl"
xmlns:local="clr-namespace:My.LogTest"
Looks like you may have made a mistake in the namespacing? LoggerControl is listed as being the namespace My, while you're importing My.LogTest and assigning it to the xml-prefix local. Change this to:
xmlns:local="clr-namespace:My"
And I think it should work. Otherwise, fix the LoggerControl declaration.

Create a custom click event handler for a WPF usercontrol which contains a button?

have you ever found a problem when assigning a click event handler for your custom WPF usercontrol with a nested button control? I do.
When you put such user control in a main window, let's say Main.xaml, the MouseLeftButtonDown doesn't work, but the PreviewMouseLeftButtonDown works like a charm.
But imagine yourself telling each developer in your team to use this event when using your usercontrol... Some usercontrols in you library has MouseLeftButtonDown, others PreviewMouseLeftButtonDown.... It's a mess don't you agree?
So I've got a solution but I want someone to see if there's some elegant way to create your custom event handler called "Click".
In my usercontrol called CustomButton.xaml.cs, I have so far:
public partial class CustomButton: UserControl
{
public CustomButton()
: base()
{
this.InitializeComponent();
}
public delegate void ClickHandler(object sender, EventArgs e);
public event EventHandler Click;
public void Button_Click(object sender, RoutedEventArgs e) {//execute daddy's button click
(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);
e.Handled = false;
}
In my CustomButton.xaml
<UserControl
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"
x:Class="YourCompany.UI.Controls.CustomButton" d:DesignHeight="72.5" d:DesignWidth="200">
<UserControl.Resources>
blablabla
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<Button Style="{DynamicResource CustomButton}"
Width="{Binding ElementName=CustomButton, Path=ActualWidth}"
Cursor="Hand" Foreground="#ffffff" FontSize="28" Margin="8,8,0,12"
HorizontalAlignment="Left"
Content="Custom Button" Click="Button_Click" />
</Grid>
Now in my Main.xaml, the caller, I have:
<Window x:Class="YourCompany.MyProject.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyProject!" Height="600" Width="800"
MinWidth="800" MinHeight="600" WindowState="Maximized" WindowStartupLocation="CenterScreen"
xmlns:bigbola="clr-namespace:YourCompany.UI.Controls;assembly=YourCompany.UI.Controls">
<mycontrols:CustomButton Name="test" MyImage=".\Images\btnOptions.png" Cursor="Hand"
Texto="See options" Click="test_Click"
Margin="168.367,176.702,253.609,0" ToolTip="See all options" Height="76.682"
VerticalAlignment="Top"></mycontrols:CustomButton>
Explanation:
in the usercontrol, when you click the nested button, it executes its parent custom "Click" handler.
Is there a elegant way to accomplish the same effect?
Going off of what mdm20 was saying... Why are you creating a UserControl (a collection of controls grouped into 1) when you could much more easily create a CustomControl (a control that extends the functionality of an existing control, such as a Button)? Assuming a Button is the only control you'd like in CustomButton, I'd highly recommend a CustomControl over what you have (a UserControl).
Example of UserControl vs CustomControl here
Hope this helps!
If your implementing a button, why not just derive from button?
To answer your question though, all you need it this.
if (Click != null) Click(this, EventArgs.Empty);
Couldn't this line:
(((sender as Button).Parent as Grid).Parent as CustomButton).Click(sender, e);
be replaced by
this.Click(sender, e);
?
Other than that though the answer depends on the exact behaviour that you want. If you want to click event of your user control to only trigger when you click on the inner button then I think you are handling it the right way. On the other hand if you want the click event to trigger whenever you click anywhere within the bounds of the user control then you are probably best styling or inheriting from the standard button control. Remember that in WPF the button's content can be any other element including another button.

Resources