您可以使用WindowChromeWPF自定義標題欄。在非客戶區添加按鈕是相當簡單的,只要你記得設定WindowChrome.IsHitTestVisibleInChrome="True".
現在,雙擊禁用的按鈕時會出現錯誤或意外/奇怪的行為。什么都不會發生,而是應用程式被最大化。
重現步驟
- 創建一個新
WPF專案。最好有針對性.NET 6。 - 將下面的代碼粘貼到
MainWindow.xaml. - 運行應用程式并雙擊該按鈕。
預期:什么都沒有發生
實際:應用已最大化
<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">
uj5u.com熱心網友回復:
雙擊標題欄會導致視窗改變其狀態,
- 啟用按鈕時,雙擊將由按鈕處理,不會傳遞到標題欄。
- 當按鈕被禁用時,雙擊將傳遞到標題欄,如果你用任何禁用的 UIElement 替換按鈕,就會發生這種情況。
所以目前的行為是正常的。
有解決方法嗎?
是的,如果您想禁用 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://www.uj5u.com/caozuo/513331.html
標籤:wpf窗口镀铬
上一篇:文本框樣式設定器中斷功能
