如何创建其他窗口继承的窗口?

时间:2012-03-25 05:46:52

标签: c# wpf visual-studio-2010 xaml

我有一个像这样的窗口

<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>

我希望我创建的每个新窗口都有这个布局。我必须为此使用资源字典。如果是这样的话?或者我是否必须做其他事情

这只是为了掌握这个概念。我将在以后使用图像和标签。

1 个答案:

答案 0 :(得分:3)

您应该声明通常在ControlTemplate中定义的ResourceDictionary。例如:

<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>

然后您应该将其添加到app.xaml中的应用程序资源:

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

在你的窗口中使用它:

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