我有一個簡單的例子來顯示這個問題。有兩個Window
其中之一是主窗口和另一個是SecondWindow。我在SecondWindow裏面放了一個大按鈕,按鈕上有一個IsMouseOver
觸發器。但是當光標移動時它不能正常工作。我使用下面的代碼來創建這個完整的例子。試試看看問題。我該如何解決它?儘管光標不在按鈕上,爲什麼IsMouseOver觸發器不起作用?
MainWindow.xaml
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" AllowsTransparency="True" WindowStyle="None">
<Grid>
<Button Content="Show Dialog" HorizontalAlignment="Left" Margin="10,71,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click" RenderTransformOrigin="-1.211,0.918"/>
</Grid>
SecondWindow.xaml
<Window x:Class="WpfApplication3.SecondWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
Background="Green" AllowsTransparency="True" WindowStyle="None">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Button Content="SAVE" Height="50" VerticalAlignment="Bottom">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Background" Value="Blue"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
</Grid>
MainWindow.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow w = new SecondWindow();
w.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
w.Owner = this;
w.ShowDialog();
}
}
問題圖片:在主窗口,而不是SecondWindow,但按鈕的背景顏色,光標不會變爲藍色,它仍然是紅色的。
這是因爲當鼠標水平地離開按鈕時,它不會擊中按鈕的直接父母,因爲窗口是無框的,並且按鈕沒有空白。在SecondWindow的上下文中,然後沒有離開按鈕並且觸發器不被執行。如果你在按鈕上設置了一定的邊距,它會起作用 - 但是對你而言可能不是最佳的? –
但它應該沒有設置保證金。我之前也試過,但除非邊距很大,否則不敏感。你認爲這可能是一個錯誤或遺忘的東西來修復語言設計師? –
@AliTor你的代碼應該和我的工作很好 – dkozl