wpf 自定义窗体 另一种实现方式

最初接触wpf时候做自定义的窗体 用来改变标题栏样式并支持拖动缩放,适用的事两个window 那种方式,网上有好多例子,但是后来觉得太麻烦了(每次都是以太麻烦为动力=。。=)

于是 找到了第二种方式 windowchrome

windowchrome 位于microsoft.windows.shell dll中是一个开源的库。

它的原理就是在窗体外面覆盖一层控件 其实和两个窗体的方案差不多。

上图:

image

其中主要要注意的有两点:

第一是 由于覆盖的问题会导致标题栏的按钮无法命中,可能导致选中或者触发不正确的问题,所以需要如下设置

<Button  shell:WindowChrome.IsHitTestVisibleInChrome="True" Style="{StaticResource CloseButtonStyle}"
                           HorizontalAlignment="Right" Margin="0,0,25,0" Width="24" Height="24"
                       Command="{x:Static shell:SystemCommands.CloseWindowCommand}"
                    CommandParameter="{Binding ElementName=SelectDialog}">
            </Button>

第二 windowchrome 默认提供了几个命令 如最大化 最小化 关闭等 可以绑定。

<Window.CommandBindings>
        <CommandBinding Command="{x:Static shell:SystemCommands.CloseWindowCommand}"
                    Executed="_OnSystemCommandCloseWindow"/>
    </Window.CommandBindings>


  private void _OnSystemCommandCloseWindow(object sender, ExecutedRoutedEventArgs e)
        {
            SystemCommands.CloseWindow((Window)e.Parameter);
        }

大笑 就到这里了

原文地址:https://www.cnblogs.com/aoldman/p/2881635.html