如何在WPF中定义窗体模板

可以在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>
posted @ 2010-01-10 21:25  陈希章  阅读(3426)  评论(1编辑  收藏  举报