Should I be using a Page, Window or UserControl - wpf

I'm developing a new desktop application that will have several views such as a dashboard, event viewer, chart viewer to name a few. Essentially the user will switch between one of these view which will cover the whole screen and not just a part of it.
I'm currently stuck on whether I should be creating a new Window, Page or UserControl for each dashboard, event viewer, chart viewer etc.
I have done some reading and understand that Pages were built for navigation which in turn lets me keep a history of the navigation so I can go back/forward. However I don't think I need that functionality for my desktop application.
So can I use either a UserControl or a Window? Or should there only be one Window per application?
Thanks

A Window has things like Title bar (including min/max/close buttons, etc) and can be used to host XAML elements, such as User Controls.
You are certainly not restricted to using one Window per Application, but some applications would choose that pattern (one window, hosting a variety of UserControls).
When you create a new WPF Application, by default your app is configured (in App.xaml) like this:
<Application x:Class="WpfApplication1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
The StartupUri property tells the app which Window to open first (you can configure this if you wish)
If you would like to logically separate your Window into pieces and do not want too much XAML in one file, you could do something like this:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition />
</Grid.RowDefinitions>
<local:HeaderUserControl Grid.Row="0" />
<local:MainSectionUserControl Grid.Row="1" />
</Grid>
</Window>
where HeaderUserControl and MainSectionUserControl are UserControls encapsulating the aspects of that Window, as needed.
If you want to show another Window, you can, in code, call Show or ShowDialog on an instance of the new Window you want to show...
Also - yes, a Page is part of a WPF Browser application, designed to be viewed in Internet Explorer.

A page is something you would use in a browser, not for a standalone application.
The Window class represents a top-level object, that is, it is not meant to be contained in another control. All the windows you see while using the Windows OS (if they were WPF application) would be created by deriving from the Window class, and you would use the Window class to create your own windows.
The UserControl class lets you create new custom controls, in case a standard control does not already exist for what you need. A UserControl can be contained inside of a window or another control, but a Window is not contained inside anything (this is the big difference!)

Related

WPF Chrome window remove gap between windows

I am building a WPF library that connects windows.
In the example the windows have the following properties:
<Window ...
Height="150"
Width="200"
WindowStyle="None"
ResizeMode="CanResize"
Background="#222222"
AllowsTransparency="False">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="1"
CornerRadius="0"/>
</WindowChrome.WindowChrome>
I use Chrome window because I need to remove the title and be able to resize.
Everything is working well except there are noticeable gaps between windows, although I want to connect them exactly next to each other.
Example layout with 2x magnifier:
https://prnt.sc/26if2vs
What does it cause, can I handle it inside the app, or it caused by the Windows system?

Draw on top of Windows Forms control

For my current project, I am required to display a PDF, and then draw on top of it. I am using Adobe Reader as a PDF viewer, as this can be hosted in a Windows Forms control which can in turn be hosted in a WPF application. However, I cannot draw over this control.
There seem to be a couple of approaches to solving this problem out there, but for the life of me I cannot seem to find a good example of a generic solution that would fit into my existing code. The most common solution I can see are Adorner/Layer/Decorators, but I can't find a way to get them into my XAML in a way that won't break the application.
My current XAML is as follows:
<Window x:Class="ThisProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ThisProject"
Title="MainWindow" Height="768" Width="1366"
WindowState="Maximized" WindowStyle="None" KeyDown="WindowKeyDown"
Loaded="WindowLoaded">
<Grid Name="PDFGrid">
<local:PDFViewerHost x:Name="PdfViewer"/>
</Grid>
</Window>
What I will need to go on top of the PDF viewer is a bunch of shapes, defined at runtime. Any suggestions as to a method that will allow me to stick those shapes on top of it would be greatly appreciated.
Thanks!

Using control templates in user controls...?

I'm really new to WPF and I'm trying to change the hover colour of a button in WPF. I've figured out I need to create a control template in order to efficiently do this, which I've been able to successfully do in a standard WPF application which has a App.XAML file, however the application that I'm using isn't a full WPF app, it's a winforms app that uses a ElementHost to link a WPF user control into the form. Soooooooo I was wondering how do I create a control template for WPF user control? I don't have a app.XAML which is where I put the control template the first time I did it, and if I try to slide the control template into any of the user control XAML it throws an error.
Thanks
When you're creating the template in App.xaml you're adding it as a resource by putting it inside the Application.Resources resource dictionary. You can do the exact same thing on any other element in XAML that represents a FrameworkElement (i.e. any control, layout panels, etc.). The basic setup is like this:
<UserControl ...>
<UserControl.Resources>
<ControlTemplate x:Key="MyCustomTemplate" .../>
</UserControl.Resources>
</UserControl>

Using ATL ActiveX control inside WPF Window application

The COM object was not created. The following is XAML code:
Title="Window1" Height="373" Width="701" Loaded="Window_Loaded">
Draw Contour
<Window.Resources>
<!--<BitmapImage x:Key="MyImageSource" UriSource="c:/cs3/TextActiveX/TestActiveX/Image.bmp"/>-->
<Image x:Key="MyImageSource" Source="C:\CS3\TestActiveX\TestActiveX\Image.bmp"/>
</Window.Resources>
On load event I check:
if (!this.imageView.Created)
and it appears that control was not created (this.ocx is null).
I understand that something wrong with registration. I tried re-register - does not work. I tried run as admin and even disable UAC and restart Vista - does not work. But if I run MFC test application or HTML script it works just fine.
Sorry text was too long and it was cut. I am using WindowsFormHost to host ATL based ActiveX control. I defined both inside XAML code.

Windows Forms Host + System.Windows.Forms.DataVisualization.Chart

Good day all
I have the following question:
I would like to use Chart from Windows Forms due to the fact that it allows to build much more types of graphical visualisation that one from WPF Toolkit does.
So, I am adding Chart control for Windows Forms as a child element into the WindowsFormsHost. But, when I run the application I and all my clients see only white area. Though, any other Windows Forms Control works great in Windows Forms Host.
What is wrong with the Chart control?
Here is the XAML code
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:CHR="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
Title="Window1" Height="300" Width="300">
<Grid>
<wfi:WindowsFormsHost x:Name="mainFGrid" >
<CHR:Chart x:Name="mainChart" />
</wfi:WindowsFormsHost>
</Grid>
</Window>
Kind regards,
Anatoliy Sova
I found kind of Workaround, basically create the chart at runtime
http://support2.dundas.com/Default.aspx?article=1331
Thanks
Shaik

Resources