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

本文关键字:窗口 其他 继承 创建 何创建 | 更新日期: 2023-09-27 18:28:51

我有一个类似的窗口

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

我希望我创建的每个新窗口都有这种布局。我必须使用资源字典吗。如果是,如何?还是我必须做其他事情

这只是为了把握概念。稍后我将使用图像和标签。

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

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

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