接收事件时闪烁标签页眉 [英] Blink tab header on receiving event

查看:29
本文介绍了接收事件时闪烁标签页眉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a tab based chat application, which a user can chat with several people in different tab items. I want to notify the user of incoming messages by blinking the tab header in case of the user is chatting with another user other than the tab which receives the message. How I can achieve this in WPF. Some example will be greatly appriciated.

最好的问候莫特萨

推荐答案

您需要为标题创建一个样式,其中包含一个动画来使标题前景闪烁/闪烁.一旦你有了它,你就可以在需要时应用它.

You need to create a style for the header that includes an animation to flash/blink the header foreground. Once you have this you can then apply this when ever needed.

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

The example below does this. You may want to modify this so set the background instead to make the whole tab flash not just the TabItems text.

<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# 代码中,您可以在需要时设置样式:

Then in the c# code you can set the style when ever needed:

   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;
            }
        }

这篇关于接收事件时闪烁标签页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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