本文介绍了在 WPF 中,我可以拥有一个带有常规最小化、最大化和关闭按钮的无边框窗口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在最大化时查看 Chrome 浏览器,它的选项卡标题就位于窗口的顶部.我可以做类似的事情吗?

If you look at the Chrome browser when maximized, it has its tab headers right at the top of the window. Can I do something similar?

推荐答案

当然可以,但您将不得不自己重新制作这些按钮(这并不难,别担心).

Absolutely, but you're going to have to remake those buttons yourself (it's not hard, don't worry).

在您的 MainWindow.xaml 中:

In your MainWindow.xaml:

<Window ...
        Title="" Height="Auto" Width="Auto" Icon="../Resources/MyIcon.ico" 
        ResizeMode="NoResize" WindowStartupLocation="CenterScreen" 
        WindowStyle="None" AllowsTransparency="True" Background="Transparent"
        ...>
    <Canvas>
       <Button /> <!-- Close -->
       <Button /> <!-- Minimize -->
       <Button /> <!-- Maximize -->
       <TabControl>
           ...
       </TabControl>
    </Canvas>
</Window>

然后,您只需将按钮和 TabControl 根据需要放置在画布上,并自定义外观.

Then you just have to place the Buttons and the TabControl as wished on the Canvas, and customize the look and feel.

.NET 4.5 中用于关闭/最大化/最小化的内置命令是 SystemCommands.CloseWindowCommand/SystemCommands.MaximizeWindowCommand/SystemCommands.MinimizeWindowCommand

The built in commands for closing/maximizing/minimizing in .NET 4.5 are SystemCommands.CloseWindowCommand/SystemCommands.MaximizeWindowCommand/SystemCommands.MinimizeWindowCommand

因此,如果您使用的是 .NET 4.5,您可以:

So if you're using .NET 4.5, you can do:

<Window ...
        Title="" Height="Auto" Width="Auto" Icon="../Resources/MyIcon.ico" 
        ResizeMode="NoResize" WindowStartupLocation="CenterScreen" 
        WindowStyle="None" AllowsTransparency="True" Background="Transparent"
        ...>
    <Window.CommandBindings>
        <CommandBinding Command="{x:Static SystemCommands.CloseWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_1" />
        <CommandBinding Command="{x:Static SystemCommands.MaximizeWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_2" />
        <CommandBinding Command="{x:Static SystemCommands.MinimizeWindowCommand}" CanExecute="CommandBinding_CanExecute_1" Executed="CommandBinding_Executed_3" />
    </Window.CommandBindings>
    <Canvas>
       <Button Command="{x:Static SystemCommands.CloseWindowCommand}" Content="Close" />
       <Button Command="{x:Static SystemCommands.MaximizeWindowCommand}" Content="Maximize" />
       <Button Command="{x:Static SystemCommands.MinimizeWindowCommand}" Content="Minimize" />
       <TabControl>
           ...
       </TabControl>
    </Canvas>
</Window>

在你的 C# 代码隐藏中:

And in your C# code-behind:

    private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }

    private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e)
    {
        SystemCommands.CloseWindow(this);
    }

    private void CommandBinding_Executed_2(object sender, ExecutedRoutedEventArgs e)
    {
        SystemCommands.MaximizeWindow(this);
    }

    private void CommandBinding_Executed_3(object sender, ExecutedRoutedEventArgs e)
    {
        SystemCommands.MinimizeWindow(this);
    }

这将使关闭/最大化/最小化与常规窗口完全一样.
当然,您可能希望使用 System.Windows.Interactivity 将 C# 移动到 ViewModel 中.

This will make close/maximize/minimize works exactly like with a regular window.
Of course, you may want to use System.Windows.Interactivity to move the C# into a ViewModel.

这篇关于在 WPF 中,我可以拥有一个带有常规最小化、最大化和关闭按钮的无边框窗口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 13:29