• 周二. 8月 9th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

如何在WPF中定义窗体模板

admin

11月 28, 2021
参考网址:https://www.cnblogs.com/chenxizhang/archive/2010/01/10/1643676.html
可以在app.xaml中定义一个ControlTemplate,指定TargetType为Window
<Application x:Class="WpfApplication1.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <Application.Resources>
        <ControlTemplate x:Key="WindowControlTemplate1" TargetType="{x:Type Window}">
            <Border 
        Background="{TemplateBinding Background}" 
        BorderBrush="{TemplateBinding BorderBrush}" 
        BorderThickness="{TemplateBinding BorderThickness}"
        >
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="0.93*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="0.21*"/>
                        <ColumnDefinition Width="0.79*"/>
                    </Grid.ColumnDefinitions>

                    <ContentPresenter 
                Grid.ColumnSpan="2" 
                Grid.Row="1" 
                Content="{TemplateBinding Content}" 
                ContentTemplate="{TemplateBinding ContentTemplate}"
                />
                    <ResizeGrip 
                HorizontalAlignment="Right" 
                x:Name="WindowResizeGrip" 
                VerticalAlignment="Bottom" 
                IsTabStop="False" 
                Visibility="Collapsed" 
                Grid.Column="1" 
                Grid.Row="2"
                />
                    <TextBlock Text="My Logo" />
                    <TextBlock Grid.Column="1" Text="My Title"/>
                    <StatusBar Height="20" Grid.ColumnSpan="2" Grid.Row="2"/>
                </Grid>
            </Border>

            <ControlTemplate.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
                        <Condition Property="WindowState" Value="Normal"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/>
                </MultiTrigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Application.Resources>
</Application>
然后在窗体中可以像下面这样使用
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Template="{DynamicResource WindowControlTemplate1}">
    
    <Button Grid.Row="1" Grid.Column="1" Content="Hello,World"></Button>
    
</Window>

发表评论

您的电子邮箱地址不会被公开。