[英]WPF Custom Window Template No Content
我们将移至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,则将得到边框全黑窗口。
正如您已经从样式中看到的那样,我希望背景为白色。 但是我所得到的只是一个“空”的窗口。 正如您从其他二传手项目中看到的那样,我想在上面加上一些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个教程中所示...
所以这是我的问题,为什么自定义内容下面什么都没有显示? 我是否需要指定某个画布,然后可以在其上为每个窗口放置控件? 考虑登录窗口,选项窗口,消息/确认窗口。 它们应该共享相同的样式,因此我们希望将窗口模板化。
要允许向窗口添加控件,您需要向ControlTemplate
添加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>
当然,需要调整ContentPresenter
的Grid.Row
和Grid.Column
设置,具体取决于您希望在何处插入窗口内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.