gpt4 book ai didi

wpf - 接收事件时闪烁标签页眉

转载 作者:行者123 更新时间:2023-12-02 15:42:58 25 4
gpt4 key购买 nike

我有一个基于选项卡的聊天应用程序,用户可以在不同的选项卡项中与几个人聊天。如果用户正在与接收消息的选项卡以外的其他用户聊天,我想通过闪烁选项卡标题来通知用户传入消息。我如何在 WPF 中实现这一点。一些例子将非常有用。

最好的问候莫特萨

最佳答案

您需要为页眉创建一个样式,其中包含一个使页眉前景闪烁/闪烁的动画。一旦你有了这个,你就可以在需要的时候应用它。

下面的例子就是这样做的。您可能想要修改它,因此设置背景而不是使整个选项卡闪烁,而不仅仅是 TabItems 文本。

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Window.Resources>
<Style x:Key="FlashingHeader" TargetType="TabItem">
<Setter Property="TabItem.HeaderTemplate">
<Setter.Value>
<DataTemplate>

<!--Make The Header -->
<TextBlock x:Name="header" Foreground ="Black" Text="{Binding}"/>

<!--Make The Background Flash-->
<DataTemplate.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard Storyboard.TargetName="header" AutoReverse="True" RepeatBehavior="Forever" Storyboard.TargetProperty="Foreground.Color">
<ColorAnimation To="Transparent" AutoReverse="True" Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</DataTemplate.Triggers>

</DataTemplate>

</Setter.Value>
</Setter>

</Style>
</Window.Resources>

<StackPanel>

<TabControl Height="150">
<TabItem x:Name="one" Header="Page One"></TabItem>
<TabItem x:Name="two" Header="Page Two"></TabItem>
<TabItem x:Name="three" Header="Page Three"></TabItem>
</TabControl>

<StackPanel Orientation="Horizontal">

<Label >Tab Number:</Label>
<TextBox x:Name="userInput" Width="80">two</TextBox>
<Button Click="StartFlash_Click">Start Flash</Button>
<Button Click="StopFlash_Click">Stop Flash</Button>

</StackPanel>


</StackPanel>
</Window>

然后在 c# 代码中,您可以在需要时设置样式:

   private void StartFlash_Click(object sender, RoutedEventArgs e)
{
TabItem ti = (TabItem)this.FindName(userInput.Text);

if (ti != null)
{
ti.SetValue(Control.StyleProperty, (Style)this.Resources["FlashingHeader"]);
}

}

private void StopFlash_Click(object sender, RoutedEventArgs e)
{
TabItem ti = (TabItem)this.FindName(userInput.Text);

if (ti != null)
{
ti.Style = null;
}
}

关于wpf - 接收事件时闪烁标签页眉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/689057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com