简体   繁体   English

如何在使用自定义窗口镶边时向WPF标题栏添加按钮?

[英]How can I add a button to the WPF caption bar while using custom window chrome?

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>

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. 我是WPF的新手,并想重构它(以便在许多按钮中使用它),但我不确定如何。

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可见的按钮,例如:

shell:WindowChrome.IsHitTestVisibleInChrome="True"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM