在Wpf中显示通知
本文关键字:通知 显示 Wpf | 更新日期: 2023-09-27 18:00:25
我想显示一个包含标签的网格,并将其显示在另一个网格的左上方。wpf代码(xml)如下:
<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:5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid.RenderTransform>
<ScaleTransform ScaleY="1" />
</Grid>
如何在按下按钮时创建此通知?我有这个的C#代码。谢谢
按下按钮时在网格上设置Grid.Visibility
。您需要一个合格的名称x:Name="MyGrid"
,然后可以从代码后面设置可见性。
如果使用MVVM之类的方法,则可以将网格的可见性数据绑定到ViewModel中的bool
(并使用DataTrigger
而不是EventTrigger
)。
编辑:
尽可能小:
<Grid x:Name="NotifyGrid" Visibility="Hidden">
<Label Content="Notification" />
</Grid>
<Button Content="Click" Click="Button_Click" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="50" />
然后在代码背后:
private void Button_Click(object sender, RoutedEventArgs e)
{
NotifyGrid.Visibility = System.Windows.Visibility.Visible;
}