在WPF
中,可以使用WindowChrome自定义标题栏。只要记得设置WindowChrome.IsHitTestVisibleInChrome="True"
,在非客户端区域添加按钮就相当简单。
现在,当一个禁用的double-clicked.按钮是时,出现了一个错误或意外/奇怪的行为。什么都不应该发生,但是应用程序是最大化的。
复制步骤
WPF
项目。最好是针对.NET 6
。MainWindow.xaml
中。Expected:什么都没发生
实战:应用程序被最大化
<Window x:Class="WpfChromeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowStyle="None" Height="450" Width="800">
<WindowChrome.WindowChrome>
<WindowChrome CaptionHeight="20" />
</WindowChrome.WindowChrome>
<Window.Template>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Width="200" Height="28" VerticalAlignment="Top"
Content="I'm disabled! DOUBLE-CLICK ME!"
WindowChrome.IsHitTestVisibleInChrome="True"
IsEnabled="False" />
</Grid>
</ControlTemplate>
</Window.Template>
</Window>
我在这里错过了什么?是虫子吗?如果是的话,有解决办法吗?
更新
被接受的答案是正确的。在WindowChromeWorker.cs(670)中可以看到它工作的原因,对UIElement.InputHitTest的调用确实会跳过任何禁用的元素。然而,在第673项上,我们发现了允许建议的解决方案的魔力:
当父元素将WindowChrome.IsHitTestVisibleInChrome
设置为true
时,回调将正确返回HTCLIENT
,从而有效地吞噬了我们的双击。
在提供的示例中,我们可以简单地将<Grid>
替换为以下内容,以获得所需的行为:
<Grid WindowChrome.IsHitTestVisibleInChrome="True">
发布于 2022-10-04 11:06:50
双击标题栏将导致窗口更改其状态,
所以现在的行为是正常的。
有解决办法吗?
是的,如果您想禁用WindowState更改,而不管按钮的IsEnabled
值如何,请使用另一个UI元素包装<Button/>
,如果该按钮被禁用,将防止双击传递到窗口的标题栏。
<ContentControl
Grid.Row="0"
Width="200"
Height="28"
VerticalAlignment="Top"
WindowChrome.IsHitTestVisibleInChrome="True">
<Button
x:Name="MyButton"
Content="I'm disabled! DOUBLE-CLICK ME!"
IsEnabled="False" />
</ContentControl>
https://stackoverflow.com/questions/73945424
复制相似问题