使用ShowDialog和无边框窗口时,IsMouseOver触发器不起作用

Ali*_*Tor 9 wpf triggers ismouseover

我有两个Windows申请.其中一个MainWindow是设置,另一个是设置.SettingsWindow当设置按钮被点击使用打开ShowDialog并设置其OwnerMainWindow.

SettingsWindow我窗口的最底部一个按钮,它的颜色变为红色时,IsMouseOverTrue蓝色False.但是当光标在MainWindow上时它不会改变.图像在下面是清楚的.我该如何解决这个问题?

CASE:光标不在SettingsWindow中,但它保持红色,没有变化.

在此输入图像描述

Xaml代码:

<Window x:Class="AltoSS.SettingsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SettingsWindow"
        Height="150"
        Width="360"
        WindowStyle="None"
        AllowsTransparency="True"
        WindowStartupLocation="CenterOwner">

  <!-- Other control codes-->
  <Button Grid.Row="2" Content="KAYDET" 
          FontSize="15"
          FontWeight="Bold"
          BorderBrush="Gray"
          BorderThickness="0,2,0,2">
    <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>
</Window>
Run Code Online (Sandbox Code Playgroud)

小智 3

好吧,经过一些研究,我找不到发生这种情况的任何逻辑原因。对我来说这更像是一个错误。因此,如果有人确切知道为什么会发生这种情况,请告诉我们!

无论如何,我已经想出了一个解决方法。基本上,我们可以使用 Show() 并添加一些代码来更接近模态行为 - 例如禁用父窗口,直到对话框关闭或用户选择“确定”或“取消”。

例子:

SettingsWindow settingsWindow = new SettingsWindow(); 
this.IsEnabled = false; //disables the main window 
settingsWindow.Owner = this; // main window is the settings window owner 
settingsWindow.Show(); 
settingsWindow.Closed += (o, e1) => { onWindowClosed(o,e1); }; // this is the close event
Run Code Online (Sandbox Code Playgroud)

订阅settingsWindow关闭事件后,我们现在可以在settingsWindow关闭时再次启用父窗口:

private void onWindowClosed(object sender, EventArgs e)
{
    this.IsEnabled = true;
}
Run Code Online (Sandbox Code Playgroud)

触发器现在可以正常工作,父窗口将被禁用,直到其子窗口关闭。