WinForms Base class equivalent in WPF
-
Tuesday, March 06, 2012 12:57 PM
Hey,
iam pretty new to WPF and at the moment im a little confused on how to create a reuseable Window like you could do it in Windows Forms by implementing a custom base class (new Forms just have to inherit from this class and get the whole functionality, style...).
The background is that i would like to have an approach where i can implement a base class which is in corprate design (like having a window header with custom close/maximize buttons and custom background color etc.) and reuse this class for all my applications.
Can anyone give me a hint on how to do this in WPF ?
All Replies
-
Tuesday, March 06, 2012 1:23 PM
You can inherit from another window (form) in WPF. The problem is that you lose the VisualTree. Therefore it is not possible to use the master page paradigm.
You can indeed create a whole bunch of user controls which you then load into your main window.
If you are new to WPF I recommend researching the VisualTree in WPF as it is a very important concept
:)
-
Tuesday, March 06, 2012 2:31 PMModerator
Hi 2bro2b,
Here is as clear a sample as I can make, to show you the principle of templating and re-using a styled base window class.
The window style is defined in app.xaml, copying the base template, but adding [for example] in our own "red rectangle" header at the top of every window.
I define a class inheriting from Window, called BaseWindow. This class sets it's own style from the App resources. It also contains the generic business logic we want to share across all our windows. In this example I put the company name in a property there, which is later used by the final application window.
You can therefore create any windows you like that in turn inherit from BaseWindow. Anything you add to your new window will be shown by the content presenter of the BaseWindow, as shown below in the style's template definition.
App.xaml
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication9.App" StartupUri="MainWindow.xaml"> <Application.Resources> <Style x:Key="BaseWindowStyle" TargetType="{x:Type Window}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Window}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition/> </Grid.RowDefinitions> <Rectangle Fill="Red" Height="50"/> <Border Grid.Row="1" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <AdornerDecorator> <ContentPresenter/> </AdornerDecorator> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </Application.Resources> </Application>BaseWindow.cs
using System.Windows; namespace WpfApplication9 { public class BaseWindow : Window { public string CompanyName { get; set; } public BaseWindow() { // Set base style this.Style = (Style)FindResource("BaseWindowStyle"); //Do generic stuff used in all windows CompanyName = "Mega Corp"; DataContext = this; } } }
MainWindow.xaml
<local:BaseWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="WpfApplication9.MainWindow" Width="300" Height="200" xmlns:local="clr-namespace:WpfApplication9"> <Grid x:Name="LayoutRoot"> <TextBlock Text="{Binding CompanyName}" FontSize="48"/> </Grid> </local:BaseWindow>
MainWindow.xaml.cs
namespace WpfApplication9 { public partial class MainWindow : BaseWindow { public MainWindow() { this.InitializeComponent(); } } }
So you see it is very simple to create a base window, with it's own style/template.
Regards,
Pete#PEJL
- Proposed As Answer by Dragan Radovac Wednesday, March 07, 2012 12:28 AM
-
Wednesday, March 07, 2012 12:28 AM
XAML guy, this is brilliant. My answer was based on a Silverlight problem I had some time ago. I'll certainly be putting this into my code library. Awesome !
rupex
-
Wednesday, March 07, 2012 3:35 AMModerator
Hi rupex,
If you have got what you need, could I close your thread as "Answered".
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
-
Wednesday, March 07, 2012 6:13 AMHey Sheldon, this is not my question but I have posted that it should be the answer as it does answer the original question.
-
Wednesday, March 07, 2012 7:55 AM
hey thanks for you support, this brought me in the right direction.
But how can i put the solution of XAML guy into a library and resuse it in other projects ? -
Thursday, March 08, 2012 5:13 AMModerator
Hi 2bro2b,
I suggest you could put your style in APP.resource, and then you could use this resource any where you want, if you want use this project as an reference, you could Right click "References" and then choose add reference, then click "Projects" tag, then add the project you want, then you could use this base window.
Best regards,
Sheldon _Xiao[MSFT]
MSDN Community Support | Feedback to us
Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked As Answer by Sheldon _XiaoModerator Tuesday, March 20, 2012 10:17 AM

