【问题标题】:WPF Custom Window Template No ContentWPF 自定义窗口模板无内容
【发布时间】:2015-09-07 15:17:47
【问题描述】:

我们即将迁移到 WPF,目前正在开发模板化窗口。

在阅读并遵循 CodeProject 和 StackOverflow 上的教程数小时后,我正在努力解决一个我认为相当简单的透明度问题。

当我运行应用程序时,我的窗口中没有“内容”。

我在 Generic.xaml 中定义了我的样式,它位于 Themes 文件夹中。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:MTApp"
                x:Class="MTApp.Themes.Generic">

<ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
    <Grid x:Name="WindowRoot">
        <Border x:Name="WindowFrame">
            <Grid Margin="0" VerticalAlignment="Top" MouseDown="Window_MouseDown">

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="25"/>
                    <ColumnDefinition Width="75*"/>
                    <ColumnDefinition Width="75"/>
                    <ColumnDefinition Width="25"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="40" />
                    <RowDefinition Height="30" />
                    <RowDefinition Height="35" />
                    <RowDefinition Height="35" />
                    <RowDefinition Height="140*" />
                </Grid.RowDefinitions>
                <Frame x:Name="background" Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="0" Background="#ddd" BorderThickness="0 0 0 1" BorderBrush="#c9c9c9"/>
                
                <Label x:Name="windowTitle" Grid.ColumnSpan="2" Content="Title Bar" VerticalAlignment="Center" Foreground="#393939" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" FontFamily="Segoe UI Regular" FontSize="12"/>
                <Grid Grid.Column="2" Grid.ColumnSpan="2" Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50"/>
                        <ColumnDefinition Width="13"/>
                        <ColumnDefinition Width="14"/> 
                        <ColumnDefinition Width="13"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="15"/>
                        <RowDefinition Height="10" />
                        <RowDefinition Height="10" />
                    </Grid.RowDefinitions>
                    <Button x:Name="minimizeBtn" Content="" Background="#39b54a" BorderThickness="0" Grid.Row="1" Grid.Column="1" Margin="3 0 0 0" Click="minimizeBtn_Click"/>
                    <Button x:Name="maximizeBtn" Content="" Background="#f8be3f" BorderThickness="0" Grid.Row="1" Grid.Column="2" Margin="3 0 0 0" Click="maximizeBtn_Click"/>
                    <Button x:Name="quitBtn" Content="" Background="#f84646" BorderThickness="0" Click="quitBtn_Click" Grid.Row="1" Grid.Column="3" Margin="3 0 0 0"/>
                </Grid>
            </Grid>
        </Border>
    </Grid>
</ControlTemplate>

<Style x:Key="SkinWindowStyle" TargetType="Window">
    <Setter Property="WindowStyle" Value="None" />
    <Setter Property="AllowsTransparency" Value="True" />
    <Setter Property="Background" Value="#ffffff" />
    <Setter Property="Opacity" Value="100" />        
    <Setter Property="ResizeMode" Value="CanResize" />
    <Setter Property="Width" Value="600" />
    <Setter Property="Height" Value="300" />
    <Setter Property="Template" Value="{StaticResource WindowTemplate}" />

    <Style.Triggers>
        <DataTrigger Binding="{Binding WindowState}" Value="Maximized">
        </DataTrigger>
    </Style.Triggers>
</Style>

它定义了许多教程中看到的“WindowStyle”和“AllowsTransparency”,如果我将“AllowsTransparancy”设置为False,我将得到一个边框全黑的窗口。

正如您已经从样式中看到的那样,我想要背景为白色。但我得到的只是一个“空”的窗口。正如您从其他 setter 项中看到的那样,我想对其施加一些 witdh。

我的 MainWindow.xaml,即实际应用程序如下所示:

<Window x:Class="MTApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Style="{DynamicResource SkinWindowStyle}"    
    Background="White"
    Height="300"
    Width="350"        
    >
<Grid Background="White" Height="100" Width="200">        
    <TextBlock Text="Test" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Background="White"/>
</Grid>

它使用 Generic.xaml 中定义的样式,一切正常。当然,背景、高度和宽度不能覆盖样式的属性,因为它是硬编码的,但仍然应该显示网格以及文本框。但这些都没有出现。

最后,Generic.xaml.cs:

    using System;
using System.Windows;
using System.Windows.Input;

namespace MTApp.Themes
{ 
    public partial class Generic : ResourceDictionary
    {

        public Generic()
        {
            InitializeComponent();
        }

        /**
        * Header Buttons Events
        **/
        private void minimizeBtn_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.WindowState = WindowState.Minimized;
        }

        private void maximizeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (Application.Current.MainWindow.WindowState == WindowState.Maximized)
            {
                Application.Current.MainWindow.WindowState = WindowState.Normal;
            }
            else
            {
                Application.Current.MainWindow.WindowState = WindowState.Maximized;
            }

        }

        private void quitBtn_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(((FrameworkElement)e.Source)).Close();
        }

        /** 
        * Window Events
        **/
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {

            if (e.ChangedButton == MouseButton.Left)
            {
                Window.GetWindow(((FrameworkElement)e.Source)).DragMove();
            }

        }

    }
}

请注意,它是从 ResourceDictionary 派生的,我无法从“Window”中获得它,如在线提供的 5 个教程中所见...

所以我的问题是,为什么我的自定义内容下方没有显示任何内容?我是否需要指定某个画布,然后我们可以在上面放置每个窗口的控件?想想登录窗口、选项窗口、消息/确认窗口。它们都应该共享相同的样式,因此我们希望将窗口模板化。

【问题讨论】:

    标签: c# wpf windows xaml


    【解决方案1】:

    要允许向窗口添加控件,您需要将ContentPresenter 控件添加到ControlTemplate。将以下内容添加到模板代码中:

    <ControlTemplate x:Key="WindowTemplate" TargetType="{x:Type Window}">
        <Grid x:Name="WindowRoot">
            <Border x:Name="WindowFrame">
                <Grid Margin="0" VerticalAlignment="Top">
    
                    <Grid.ColumnDefinitions>
                        ...
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        ...
                    </Grid.RowDefinitions>
    
                    ... header controls ...
    
                    <ContentPresenter Grid.Column="0" Grid.ColumnSpan="4" Grid.Row="1"/>
                </Grid>
            </Border>
        </Grid>
    </ControlTemplate>
    

    当然,ContentPresenterGrid.RowGrid.Column 设置需要根据您希望窗口内容插入的位置进行调整。

    【讨论】:

    • 哇,这么简单。谢谢 !像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-16
    相关资源
    最近更新 更多