私はこの問題を示す簡単な例があります。いずれかがMainWindowであり、もう1つがSecondWindowである2つのWindow
があります。私は大きいボタンを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