【问题标题】:Question on: Creating Custom Window Chrome in WPF有关问题:在 WPF 中创建自定义窗口 Chrome
【发布时间】:2020-07-18 20:16:18
【问题描述】:

在我的WPF 应用程序中,我正在尝试创建一个Custom Chrome Window [参考:WindowChrome]。我关注了Restyle Your Window 文章。我在我的项目中创建了Resource Dictionary file 及其代码隐藏,如下所示。然后我在 app.xaml 文件中引用了资源字典(如下所示)。如您所见,resource dictionary 文件中Style 标记的x:key 值为CustomWindowStyle问题:如何为窗口分配样式CustomWindowStyle

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

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

<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>

更新

App.xaml

根据用户@Simon Stanford的建议here:您所要做的就是在您的app.xaml 文件中引用资源字典,然后将样式分配给窗口CustomWindowStyle。所以我引用了资源字典如下。 问题现在是:如何为 Window 分配 Style 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 to create custom window chrome in wpf?
  • @TomJoney 您建议的链接中的用户@Simon Stanfordresponse 几乎正在做我所做的事情。但该用户建议:您所要做的就是引用 app.xaml 文件中的资源字典 and then assign the Window the Style CustomWindowStyle。他建议的最后一部分是我正在寻找的方法。有什么建议吗?
  • 不太确定你没有从那里得到什么,但似乎你对 ResourceDictionaries 的知识有一定的差距,所以我建议你阅读一下:docs.microsoft.com/en-us/windows/uwp/design/… 以获得有关方面的帮助此事及帮助参考。
  • @TomJoney 您的链接提供了对该主题的一般理解。但是,它的子链接Every FrameworkElement can have a ResourceDictionary 一起来自@thatfuy 的回复帮助我解决了这个问题。由于Window 也是FrameworkElement ,因此您将其属性设置为Style="{StaticResource CustomWindowStyle}",因为用户@thatguy 在下面的MainWindow.xaml 代码中显示。感谢您提供帮助。
  • 我很高兴你已经弄明白了——起初,很难解释你需要什么帮助,所以我提供了一个链接,我认为这会让你更加理解。干杯。

标签: c# wpf xaml


【解决方案1】:

如何为窗口分配样式 CustomWindowStyle?

将您的MyResourceDictionaryWindowStyle 合并到App.xaml。请参见下面的示例。您的项目的名称可能与 MyWPFProject.App 不同,资源字典的 Source 取决于您的自定义资源字典在项目中的实际位置,因此您应该相应地调整两者,以及您的 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>

然后在您的窗口中将样式设置为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>

【讨论】:

  • 我添加了一个 UPDATE 部分,其中包含我的 App.xaml 文件内容和评论。这行得通吗?
猜你喜欢
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 2011-03-08
  • 2021-06-18
相关资源
最近更新 更多