deception1
deception1

Reputation: 1767

How do i create a window that other windows inherit from?

i have a window which is like this

<Window x:Class="pharmacy_Concept.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <Button Content="Login" Height="34" HorizontalAlignment="Left" Margin="12,241,0,0" Name="loginbutton" VerticalAlignment="Top" Width="129" Click="loginbutton_Click" />
        <Button Content="Exit" Height="34" HorizontalAlignment="Left" Margin="362,241,0,0" Name="Exitbutton" VerticalAlignment="Top" Width="129" Click="Exitbutton_Click" />
    </Grid>
</Window>

I want every new window i have created to have this layout.Do i have to use a resource dictionary for this.If so How?Or do i have to do something else

This is just to grasp the concept.I will be using images and lables later.

Upvotes: 3

Views: 172

Answers (1)

ionden
ionden

Reputation: 12786

You should declare a ControlTemplate that you usually define in a ResourceDictionary. For example:

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

<Style x:Key="{x:Type Window}" TargetType="{x:Type Window}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Window}">
                <Grid Background="Red">
                    <Button Content="Login" Height="34" HorizontalAlignment="Left" Margin="12,241,0,0" Name="loginbutton" VerticalAlignment="Top" Width="129" Click="loginbutton_Click" />
                    <Button Content="Exit" Height="34" HorizontalAlignment="Left" Margin="362,241,0,0" Name="Exitbutton" VerticalAlignment="Top" Width="129" Click="Exitbutton_Click" />
                </Grid>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then you should add this to Application resources in app.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Window.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

And in your Window use it like this:

 Style="{StaticResource {x:Type Window}}"

Upvotes: 3

Related Questions