WPF 自定义窗口模板 无内容 [英] WPF Custom Window Template No Content

查看:23
本文介绍了WPF 自定义窗口模板 无内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们即将转向 WPF,目前正在开发模板化窗口.

We are about to move to WPF and are currently working on a templated window.

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

After hours of reading and following tutorials on CodeProject and StackOverflow I am struggling with a, what I believe rather simple, problem in regards of transparency.

当我运行应用程序时,没有内容";在我的窗口.

When I run the app, there is no "content" in my window.

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

I have my Style defined in Generic.xaml, which is located in the Themes folder.

<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"和允许透明度"正如在许多教程中看到的那样,如果我设置了AllowsTransparancy"为 False 我会得到一个边框全黑的窗口.

It defines "WindowStyle" and "AllowsTransparency" as seen in many tutorials, if I set "AllowsTransparancy" to False I will get a border full black window.

正如您已经从样式中看到的,我想要背景为白色.但我得到的只是一个空的"窗户.我想强加一些 witdh 到它上面,正如你从其他 setter 项中看到的那样.

As you can see from the style already, I want to have the background white. But All I get is an "empty" window. I wanted to force some witdh onto it, as you can see from the other setter items.

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

My MainWindow.xaml, which is the actuall app looks like this:

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

It uses the style defined in Generic.xaml, which all works fine. Sure, the Background, Height and Width cannot overwrite the style's properties, because its hard coded, but still, the Grid should be shown as well as the text box. But none of these appear.

最后,Generic.xaml.cs:

Lastly, the 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 个教程所示...

Mind you, its derived from ResourceDictionary, I could not get it working from "Window" as seen in the 5 tutorials available online...

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

So here my question, why is nothing shown below my custom content ? Do I need to specify a certain canvas on which we can then put our controls for each window ? Think login window, options window, message/confirm window. They should all share the same style, and hence we want to have the window templated.

推荐答案

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

To allow adding controls to the window, you need to add a ContentPresenter control to the ControlTemplate. Add the following to the template code:

<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 设置需要调整,这取决于你在哪里就像要插入的窗口内容.

Of course, the ContentPresenter's Grid.Row and Grid.Column settings need to be adapted, depending on where you'd like the window content to be inserted.

这篇关于WPF 自定义窗口模板 无内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆