Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6606731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:25:38+00:00 2026-05-25T19:25:38+00:00

Is there any recommended way with WPF to create a common window style to

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:25:38+00:00Added an answer on May 25, 2026 at 7:25 pm

    One way to do it would be a new custom control, let’s call it DialogShell:

    namespace Test.Dialogs
    {
        public class DialogShell : Window
        {
            static DialogShell()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogShell), new FrameworkPropertyMetadata(typeof(DialogShell)));
            }
        }
    }
    

    This now needs a template which would normally be defined in Themes/Generic.xaml, there you can create the default structure and bind the Content:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Test.Dialogs">
        <Style TargetType="{x:Type local:DialogShell}" BasedOn="{StaticResource {x:Type Window}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type local:DialogShell}">
                        <Grid Background="{TemplateBinding Background}">
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <!-- This ContentPresenter automatically binds to the Content of the Window -->
                            <ContentPresenter />
                            <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="5" HorizontalAlignment="Right">
                                <Button Width="100" Content="OK" IsDefault="True" />
                                <Button Width="100" Content="Cancel" IsCancel="True" />
                            </StackPanel>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    

    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:

    <diag:DialogShell x:Class="Test.Dialogs.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:diag="clr-namespace:Test.Dialogs"
            Title="Window1" Height="300" Width="300">
        <Grid>
            <TextBlock Text="Lorem Ipsum" />
        </Grid>
    </diag:DialogShell>
    
    namespace Test.Dialogs
    {
        public partial class Window1 : DialogShell
        {
            public Window1()
            {
                InitializeComponent();
            }
        }
    }
    

    Event wiring example (not sure if this is the “correct” approach though)

    <Button Name="PART_OKButton" Width="100" Content="OK" IsDefault="True" />
    <Button Name="PART_CancelButton" Width="100" Content="Cancel" IsCancel="True" />
    
    namespace Test.Dialogs
    {
        [TemplatePart(Name = "PART_OKButton", Type = typeof(Button))]
        [TemplatePart(Name = "PART_CancelButton", Type = typeof(Button))]
        public class DialogShell : Window
        {
            //...
    
            public DialogShell()
            {
                Loaded += (_, __) =>
                    {
                        var okButton = (Button)Template.FindName("PART_OKButton", this);
                        var cancelButton = (Button)Template.FindName("PART_CancelButton", this);
                        okButton.Click += (s, e) => DialogResult = true;
                        cancelButton.Click += (s, e) => DialogResult = false;
                    };
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any recommended way to open a file using Symfony? I'm trying to
Is there any recommended way to do multiple string substitutions other than doing replace
Is there a recommended way to check if an email sent by an application
Beyond the official documentation, are there any recommended resources for learning to build jQuery
Is there any way to check whether a file is locked without using a
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there any known way of listing the WMI classes and their properties available
and I would like to know if there is any way to stop a
what would be the recommended way for my main window to communicate with his
I notice in several API's, that you may create a struct which is used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.