IsMouseOver 触发器在使用 ShowDialog 和无边框窗口时不起作用 [英] IsMouseOver trigger doesn't work when using ShowDialog and borderless window

查看:17
本文介绍了IsMouseOver 触发器在使用 ShowDialog 和无边框窗口时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Windows 用于应用程序.其中之一是 MainWindow ,另一个用于设置.当使用 ShowDialog 并将其 Owner 设置为 MainWindow 单击设置按钮时,会打开 SettingsWindow.

I have two Windows for an application. One of them is MainWindow and the other is for settings. SettingsWindow opens when settings button is clicked by using ShowDialog and setting its Owner to MainWindow.

SettingsWindow 窗口的最底部有一个按钮,当 IsMouseOver 时,它会将颜色更改为 红色>Trueblue 表示 False.但是当光标在 MainWindow 上时它不会改变.为了清楚起见,下图.我该如何解决这个问题?

On the SettingsWindow I have a button at the very bottom of the window and it changes the color to red when IsMouseOver is True and blue for False. But it doesn't change when the cursor is over the MainWindow. The image is below to be clear. How can I fix this problem?

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

CASE: The cursor is out of SettingsWindow but it keeps the red color, no change.

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>

推荐答案

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

Alright, after doing some research, I couldn't find any logical reason for this to occur. It seems more like a bug to me. So if anyone knows exactly why this happens, let us know!

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

Anyway, I've come up with a workaround. Basically, we can use Show() and add some code to get closer to a modal behavior - like disabling the parent window until the dialog gets closed or the user has selected OK or Cancel for instance.

示例:

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

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

After subscribing for the settingsWindow closed event, we can now enable the parent window again when settingsWindow gets closed:

private void onWindowClosed(object sender, EventArgs e)
{
    this.IsEnabled = true;
}

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

Triggers will now work correctly and parent window gets disabled until its child is closed.

这篇关于IsMouseOver 触发器在使用 ShowDialog 和无边框窗口时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆