代码之家  ›  专栏  ›  技术社区  ›  nam

问题:在WPF中创建自定义窗口浏览器

  •  0
  • nam  · 技术社区  · 5 年前

    WPF 我正在尝试创建一个应用程序 Custom Chrome Window WindowChrome] . 我跟着 Restyle Your Window Resource Dictionary file x:key 价值 Style 加入 resource dictionary 文件是 CustomWindowStyle . 问题: 自定义窗口样式 ?

    :

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

    <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文件中的资源字典,然后为窗口指定样式CustomWindowsStyle。因此,我引用了以下资源字典。 现在是:如何为窗口指定样式CustomWindowsStyle?

    <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>
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   thatguy    5 年前

    合并你的 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>