接收事件时闪烁选项卡标题

1 wpf

我有一个基于标签的聊天应用程序,用户可以在不同的标签项中与几个人聊天.我希望通过闪烁标签标题来通知用户传入的消息,以防用户与接收消息的标签以外的其他用户聊天.我如何在WPF中实现这一目标.一些例子将非常受欢迎.

最诚挚的问候Morteza

Joh*_*ohn 5

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

以下示例执行此操作.您可能想要修改它,因此设置背景而不是使整个选项卡闪烁而不仅仅是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>
Run Code Online (Sandbox Code Playgroud)

然后在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;
            }
        }
Run Code Online (Sandbox Code Playgroud)