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

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

问题描述

我有两个 Windows 用于一个应用程序。其中之一是 MainWindow ,另一个用于设置。通过使用 ShowDialog 并设置其 Owner SettingsWindow $ c>到 MainWindow

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 True 蓝色 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?

案例:光标在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()并添加一些代码以更接近模态行为-例如禁用父窗口,直到对话框关闭或用户选择了确定或取消。

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关闭事件后,我们现在可以启用父项设置窗口关闭后再次打开窗口:

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.

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

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