简体   繁体   English

有关问题:在 WPF 中创建自定义 Window Chrome

[英]Question on: Creating Custom Window Chrome in WPF

In my WPF app I'm trying to create a Custom Chrome Window [Ref: WindowChrome] .在我的WPF应用程序中,我正在尝试创建Custom Chrome Window [参考: WindowChrome] I followed Restyle Your Window article.我跟着Restyle Your Window文章。 I created Resource Dictionary file along with its code-behind in my project, as shown below.我在项目中创建了Resource Dictionary file及其代码隐藏,如下所示。 I then referenced the resource dictionary in the app.xaml file (also shown below).然后我在 app.xaml 文件中引用了资源字典(也如下所示)。 As you can see, the x:key value of the Style tag in resource dictionary file is CustomWindowStyle .如您所见, resource dictionary文件中Style标签的x:key值为CustomWindowStyle Question : How do I assign the Window the Style CustomWindowStyle ?问题:如何为 Window 分配样式CustomWindowStyle

MyResourceDictionaryWindowStyle.xaml : MyResourceDictionaryWindowStyle.xaml

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

    <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CaptionHeight="30"
                              CornerRadius="4"
                              GlassFrameThickness="0"
                              NonClientFrameEdges="None"
                              ResizeBorderThickness="5"
                              UseAeroCaptionButtons="False" />
            </Setter.Value>
        </Setter>
        <Setter Property="BorderBrush" Value="Black" />
        <Setter Property="Background" Value="Gray" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Grid>
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="5,30,5,5">
                            <AdornerDecorator>
                                <ContentPresenter />
                            </AdornerDecorator>
                        </Border>

                        <DockPanel Height="30"
                                   VerticalAlignment="Top"
                                   LastChildFill="False">

                            <TextBlock Margin="5,0,0,0"
                                       VerticalAlignment="Center"
                                       DockPanel.Dock="Left"
                                       FontSize="16"
                                       Foreground="White"
                                       Text="{TemplateBinding Title}" />

                            <Button x:Name="btnClose"
                                    Width="15"
                                    Margin="5"
                                    Click="CloseClick"
                                    Content="X"
                                    DockPanel.Dock="Right"
                                    WindowChrome.IsHitTestVisibleInChrome="True" />


                            <Button x:Name="btnRestore"
                                    Width="15"
                                    Margin="5"
                                    Click="MaximizeRestoreClick"
                                    Content="#"
                                    DockPanel.Dock="Right"
                                    WindowChrome.IsHitTestVisibleInChrome="True" />

                            <Button x:Name="btnMinimize"
                                    Width="15"
                                    Margin="5"
                                    VerticalContentAlignment="Bottom"
                                    Click="MinimizeClick"
                                    Content="_"
                                    DockPanel.Dock="Right"
                                    WindowChrome.IsHitTestVisibleInChrome="True" />
                        </DockPanel>

                    </Grid>

                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MyResourceDictionaryWindowStyle.xaml.cs : MyResourceDictionaryWindowStyle.xaml.cs

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

namespace MyWPFProject
{
    public partial class WindowStyle : ResourceDictionary
    {
        public WindowStyle()
        {
            InitializeComponent();
        }

        private void CloseClick(object sender, RoutedEventArgs e)
        {
            var window = (Window)((FrameworkElement)sender).TemplatedParent;
            window.Close();
        }

        private void MaximizeRestoreClick(object sender, RoutedEventArgs e)
        {
            var window = (Window)((FrameworkElement)sender).TemplatedParent;
            if (window.WindowState == System.Windows.WindowState.Normal)
            {
                window.WindowState = System.Windows.WindowState.Maximized;
            }
            else
            {
                window.WindowState = System.Windows.WindowState.Normal;
            }
        }

        private void MinimizeClick(object sender, RoutedEventArgs e)
        {
            var window = (Window)((FrameworkElement)sender).TemplatedParent;
            window.WindowState = System.Windows.WindowState.Minimized;
        }
    }
}

MainWindow.xaml :主窗口.xaml

<Window x:Class="MyWPFProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
       <Button x:Name="btnTest" Content="Test" Click="btnTest_Click"/>
       ......
       ......
    </Grid>
</Window>

UPDATE :更新

App.xaml : App.xaml :

According to user @Simon Stanford 's suggestion here : All you have to do is reference the resource dictionary in your app.xaml file and then assign the Window the Style CustomWindowStyle.根据用户@Simon Stanford建议:您所要做的就是在您的 app.xaml 文件中引用资源字典,然后为 Window 分配样式 CustomWindowStyle。 So I have referenced the Resource Dictionary as follows.所以我引用了资源字典如下。 Question now is: How do I assign the Window the Style CustomWindowStyle?现在的问题是:如何为 Window 分配样式 CustomWindowStyle?

<Application x:Class="MyWOFProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:MyWOFProject"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary Source="MyResourceDictionaryWindowStyle.xaml"/>
    </Application.Resources>
</Application>

How do I assign the Window the Style CustomWindowStyle?如何为 Window 分配样式 CustomWindowStyle?

Merge your MyResourceDictionaryWindowStyle in App.xaml .App.xaml中合并您的MyResourceDictionaryWindowStyle See the example below.请参见下面的示例。 Your project might have a different name than MyWPFProject.App and the Source of the resource dictionary depends on where your custom resource dictionary is actually located in the project, so you should adapt both accordingly, as well as the StartupUri for your main window.您的项目可能具有与MyWPFProject.App不同的名称,并且资源字典的Source取决于您的自定义资源字典在项目中的实际位置,因此您应该相应地调整两者,以及您的主StartupUri的 StartupUri。

<Application x:Class="MyWPFProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
   <Application.Resources>
      <ResourceDictionary>
         <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MyWPFProject;component/MyResourceDictionaryWindowStyle.xaml"/>
         </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
   </Application.Resources>
</Application>

Then just set the style as StaticResource in your window.然后只需在 window 中将样式设置为StaticResource

<Window x:Class="MyWPFProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Style="{StaticResource CustomWindowStyle}"
        Title="MainWindow" Height="450" Width="800">
    <!-- ...your XAML code -->
</Window>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM