Is there any recommended way with WPF to create a common window style to be used across an application? I have several dialogs that appear in my app, and I would like them all to be styled the same (same window border, ok/cancel button position, etc) and simply have different 'content' in each, depending on the situation. So, one dialog might have a list box in it, one might have a textbox, and so on.
I understand how to make base .cs usercontrol files, but I can't for the life of me work out a good way to create a single window which can host different content when launched?
Cheers, rJ
You can use define a style in App.Xaml that targets all windows.
This is a sample of how your App.Xaml may look like:
Then for more advanced scenarios you may need to set the ControlTemplate for your Window.
One way to do it would be a new custom control, let's call it
DialogShell
:This now needs a template which would normally be defined in
Themes/Generic.xaml
, there you can create the default structure and bind theContent
:This is just an example, you probably want to hook up those buttons with custom events and properties you need to define in the cs-file.
This shell then can be used like this:
Event wiring example (not sure if this is the "correct" approach though)
To add to H.B.'s very helpful post, you may want to connect your event handlers in the loaded event as he's done but instead of using anonymous methods or lambda expressions, consider connecting them to protected virtual methods which can be overridden in the derived class should the functionality need to vary. In my case, I created a base data entry form which has buttons for saving and cancelling:
The save functionality is then overridden in each derived class to save the specific entity:
Create a Xaml Form template and add the template to the VS Installed ItemTemplates directory.
1) create a wpf xaml and xaml.cs file that has all the desired components wanted for a new form added to your application. In my case I wanted the title and toolbar buttons.
2) test the new xaml files through the current system flow.
3) copy xaml / xaml.cs to temp location and rename both the filenames to something you want to be recognized as a good template name. a) Change first line within xaml file to -- Window x:Class="$rootnamespace$.$safeitemname$"
b) Make 3 changes within xaml.cs file to ensure the new name will be copied when using the template - -- namespace $rootnamespace$ (//dynamic namespace name) -- public partial class $safeitemname$ (//dynamic class name) -- public $safeitemname$() (//dynamic constructor name)
4) Now create a vstemplate file: ie. MyTemplate.vstemplate with the following content:
5) Once you have all these files, zip the files and place the zip file under the ....\Documents\Visual Studio 2012\Templates\ItemTemplates\WPF directory. Now you can go into VS2012 and use the ADD\New feature to see the template, select and rename as in the normal process. The template can be used in the same way for VS2010 by placing the zip file under the 2010 Templates Wpf directory.
The Logo file should be included in the zip file as well or if you don't have a file then remove that line from the MyTemplate.vstemplate file.
Creating a cusotm object , which is derived from Window Class..
http://maffelu.net/wpf-window-inheritance-problems-and-problems/