1
我正在試圖製作一個通知彈出窗口,以顯示某些事件。每件事情都進展順利,但問題是我無法關閉該通知彈出窗口,因爲它仍然保留在任務欄中,但在顯示幾秒鐘後隱藏。任何幫助將不勝感激。如何在WPF中通過觸發器關閉窗口
XAML文件如下:
<Window
x:Class="SQLExample.NotificationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Notification Popup" Width="300" SizeToContent="Height"
WindowStyle="None" AllowsTransparency="True" Background="Transparent">
<Grid RenderTransformOrigin="0,1" x:Name="NotificationWindowsss">
<!-- Notification area -->
<Border BorderThickness="1" Background="White" BorderBrush="Black" CornerRadius="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="4*"/>
<RowDefinition Height="7"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Border CornerRadius="0,0,0,0" Background="SkyBlue">
<Grid>
<Button HorizontalAlignment="Right" Margin="5,5,5,5" Click="Button_Click" Grid.Row="0" Height="15" Width="15" Foreground="White">
<Button.Template>
<ControlTemplate>
<Grid>
<Ellipse>
<Ellipse.Fill>
<ImageBrush ImageSource="Images\Close-icon.png"/>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter Content="x" HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock TextWrapping="Wrap" Margin="10" Grid.Row="0" HorizontalAlignment="Left">
<Bold>Notification data</Bold>
</TextBlock>
</Grid>
</Border>
</Grid>
<StackPanel Margin="10" Grid.Row="1">
<TextBlock TextWrapping="Wrap">
Something just happened and you are being notified of it.
</TextBlock>
</StackPanel>
<Grid Grid.Row="2" Background="SkyBlue"/>
</Grid>
</Border>
<!-- Animation -->
<Grid.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleY)">
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0.5" Value="1"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:2" Value="1"/>
<SplineDoubleKeyFrame KeyTime="0:0:4" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
<SplineDoubleKeyFrame KeyTime="0:0:7" Value="0"/>
<SplineDoubleKeyFrame KeyTime="0:0:0" Value="1"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Grid.Triggers>
<Grid.RenderTransform>
<ScaleTransform ScaleY="1" />
</Grid.RenderTransform>
</Grid>
</Window>
和xaml.cs在這裏。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace SQLExample
{
public partial class NotificationWindow : Window
{
public NotificationWindow()
{
InitializeComponent();
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
//var workingArea = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
var workingArea = System.Windows.SystemParameters.WorkArea;
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var corner = transform.Transform(new Point(workingArea.Right, workingArea.Bottom));
this.Left = corner.X - this.ActualWidth-10;
this.Top = corner.Y - this.ActualHeight;
}));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
事情我想要做的是,當通知彈出窗口消失或技術意義上得到「透明度」等於零,那麼就應該關閉自己,我是想通過觸發器來做到這一點,但是,解決方案無法得到。引導我如何做到這一點或其他簡單的方法來做到這一點。
預先感謝您。
如果你想要一個純粹的xaml解決方案,[見這裏](http://stackoverflow.com/questions/16573960/how-to-bind-to-windows-close-button-the-x-button)(這不是公認的答案,但工作原理也是如此。) –
非常感謝您的回覆,但我認爲您告訴我的解決方案與按鈕按下相關,我的意思是用戶必須按按鈕關閉窗口,而且我已經在做我的通知關閉按鈕。我想要自動關閉窗口,就像我想到的那樣,當窗口的**不透明**變成零**時,窗口通過在不透明度等級零上觸發觸發器自動關閉。 –