我如何添加一个按钮WPF标题栏,同时使用自定义窗口镶边? [英] How can I add a button to the WPF caption bar while using custom window chrome?

查看:327
本文介绍了我如何添加一个按钮WPF标题栏,同时使用自定义窗口镶边?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的按钮模板,其中按钮通常看起来像一个水平线上,但是当鼠标悬停时,按钮会显示一个矩形颜色填充后面。这是我的代码,但我似乎无法得到触发火灾。

I'm attempting to create a simple button template in which the button normally looks like a single horizontal line, but when moused over, the button will display a "rectangle" color fill behind it. This is the code I have, but I can't seem to get the triggers to fire.

<Window x:Class="TestStylingWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="36" GlassFrameThickness="0 0 0 1" ResizeBorderThickness="5" />
    </WindowChrome.WindowChrome>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="36" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Button Height="36" Width="36" 
                    HorizontalAlignment="Center" VerticalAlignment="Center"
                Grid.Row="0">
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <Rectangle x:Name="PART_ButtonBackgroundRectangle" Fill="LightGray" Width="36" Height="36" Opacity="0" />
                        <Path x:Name="PART_ButtonPath" Data="M10,26 L26,26" Stroke="DarkGray" StrokeThickness="1.5" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger SourceName="PART_ButtonBackgroundRectangle" Property="IsMouseOver" Value="True">
                            <Setter TargetName="PART_ButtonBackgroundRectangle" Property="Opacity" Value="1" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </Grid>
</Window>



有谁知道为什么我的触发器将无法正常工作,或者一个更好的方式来做到这一点?我是那种新的WPF,想重构了这一点(以便在许多按钮使用),但我不知道该如何。

Does anyone know why my trigger won't work, or perhaps a better way to do this? I'm sort of new to WPF and would like to refactor this out (so as to use it in many buttons), but am unsure how.

更新:确定,所以我想它似乎是因为该按钮是窗口的标题栏之内。有没有解决这个好办法?也许一个方法来设置点击/悬停优先按钮?

Update: Ok, so I guess it appears to be because the button is within the caption bar of the window. Is there a good way around this? Maybe a way to set click/hover priority to the button?

推荐答案

您需要设置的HitTest你的按钮可见,如

You need to set Hittest visible of your button, such as:

shell:WindowChrome.IsHitTestVisibleInChrome="True"

这篇关于我如何添加一个按钮WPF标题栏,同时使用自定义窗口镶边?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆