闪烁标签自定义标题

时间:2018-08-09 18:17:40

标签: c# wpf

我一直在搜索,但是找不到很多,我想让我的任何动态标签页眉背景闪烁/闪烁,当我将它们称为Flash时。我找到了此解决方案,但我有点想绑定到wpf并触发它,所以我做不到。

我找到的解决方案是(Blink tab header on receiving event)。

我将在下面发送部分代码。

<TabControl x:Name="tabControlBTC" MouseLeftButtonDown="tabControlBTC_MouseLeftButtonDown" MouseRightButtonDown="tabControlBTC_MouseRightButtonDown" MouseDoubleClick="tabControlBTC_MouseDoubleClick" 
                TabStripPlacement="Left" Grid.Row="1" Grid.ColumnSpan="2" SelectionChanged="tabControlBTC_SelectionChanged">
        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Width" Value="240"/>
                <Setter Property="Height" Value="40"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type TabItem}">
                            <Grid>
                                <Border Width="auto" Name="Border" Margin="3,0,0,0" CornerRadius="0" SnapsToDevicePixels="True">
                                    <ContentPresenter VerticalAlignment="Center" ContentSource="Header" Margin="12,2,12,2"/>
                                </Border>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True" SourceName="Border">
                                <Setter TargetName="Border" Property="Background" Value="#EDF5FA" />
                                <Setter TargetName="Border" Property="BorderThickness" Value="4, 0, 0, 0"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="DarkGray"/>
                                <Setter TargetName="Border" Property="Margin" Value="3,0,0,0"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Foreground" Value="Black"/>
                                <Setter TargetName="Border" Property="Background">
                                    <Setter.Value>
                                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                            <GradientStop Color="#DAE9F3" Offset="0.432" />
                                            <GradientStop Color="#DAE9F3" Offset="0.433" />
                                        </LinearGradientBrush>
                                    </Setter.Value>
                                </Setter>
                                <Setter TargetName="Border" Property="BorderThickness" Value="0"/>
                                <Setter TargetName="Border" Property="BorderBrush" Value="DarkGray"/>
                                <Setter TargetName="Border" Property="Margin" Value="0"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            </Style>
        </TabControl.ItemContainerStyle>
        <TabControl.Template>
            <ControlTemplate TargetType="TabControl">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <ScrollViewer HorizontalScrollBarVisibility="Disabled" Background="#F4F4F5"  VerticalScrollBarVisibility="Hidden" FlowDirection="LeftToRight">
                        <TabPanel x:Name="HeaderPanel" Panel.ZIndex ="0" KeyboardNavigation.TabIndex="1" IsItemsHost="true"/>
                    </ScrollViewer>
                    <ContentPresenter x:Name="PART_SelectedContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="SelectedContent" Grid.Column="1"/>
                </Grid>
            </ControlTemplate>
        </TabControl.Template>

以及如何动态创建tabitems标头。

UserControltest1 NewUserControlPage = new UserControltest1(contactNumber, username, jsonSellorBuy, 2);

                                        newTab = new TabItem();
                                        newTab.TabIndex = tabControlBTC.Items.Count + 1;
                                        newTab.Name = "tab" + contactNumber;

                                        PathGeometry CircleGeometry = new PathGeometry();
                                        CircleGeometry.FillRule = FillRule.Nonzero;
                                        PathFigureCollectionConverter pfcc = new PathFigureCollectionConverter();
                                        CircleGeometry.Figures = (PathFigureCollection)pfcc.ConvertFrom("M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z");
                                        System.Windows.Shapes.Path PathCirle = new System.Windows.Shapes.Path();
                                        PathCirle.Fill = randomColor2();
                                        PathCirle.Data = CircleGeometry;

                                        var CanvasCirle = new Canvas() { Width = 24, Height = 24 };

                                        TextBlock textBlockCircle = new TextBlock();
                                        textBlockCircle.Text = username[0].ToString().ToUpper();
                                        textBlockCircle.Foreground = new SolidColorBrush(Colors.Black);
                                        textBlockCircle.Margin = new Thickness(0,3,0,3);
                                        textBlockCircle.FontWeight = FontWeights.DemiBold;
                                        textBlockCircle.TextAlignment = TextAlignment.Center;
                                        textBlockCircle.Width = CanvasCirle.Width;
                                        textBlockCircle.HorizontalAlignment = HorizontalAlignment.Center;

                                        CanvasCirle.Children.Add(PathCirle);
                                        CanvasCirle.Children.Add(textBlockCircle);

                                        var viewBoxCircle = new Viewbox() { Width = 32, Height = 32, Margin = new Thickness(0, 0, 5, 0) };
                                        viewBoxCircle.Child = CanvasCirle;

                                        var stackPanel = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(-6, 0, 0, 0) };

                                        var Buttoncircle = new Button() { Background = Brushes.Transparent, Margin = new Thickness(60, 0, 0, 0), BorderThickness =  new Thickness(0) };
                                        PathGeometry ButtonGeometry = new PathGeometry();
                                        ButtonGeometry.FillRule = FillRule.Nonzero;
                                        PathFigureCollectionConverter pfccc = new PathFigureCollectionConverter();
                                        ButtonGeometry.Figures = (PathFigureCollection)pfccc.ConvertFrom("M13.46,12L19,17.54V19H17.54L12,13.46L6.46,19H5V17.54L10.54,12L5,6.46V5H6.46L12,10.54L17.54,5H19V6.46L13.46,12Z");
                                        System.Windows.Shapes.Path PathCirleButton = new System.Windows.Shapes.Path();
                                        PathCirleButton.Fill = Brushes.Black;
                                        PathCirleButton.Data = ButtonGeometry;
                                        Buttoncircle.Name = $"tab{contactNumber}";

                                        var CanvasCirleButton = new Canvas() { Width = 24, Height = 24 };
                                        CanvasCirleButton.Children.Add(PathCirleButton);

                                        var viewBoxCircleButton = new Viewbox() { Width = 18};
                                        viewBoxCircleButton.Child = CanvasCirleButton;
                                        Buttoncircle.Content = viewBoxCircleButton;
                                        Buttoncircle.Click += (s, e) => { Image_MouseDown(Buttoncircle); };

                                        var stack = new StackPanel() { VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(0, 0, 0, 0) };
                                        stack.Children.Add(new TextBlock() { Text = username, FontSize = 15 });
                                        stack.Children.Add(new TextBlock() { Text = $"Trade #{contactNumber}", FontSize = 11 });
                                        stackPanel.Children.Add(viewBoxCircle);
                                        stackPanel.Children.Add(stack);
                                        stackPanel.Children.Add(Buttoncircle);

                                        newTab.Content = NewUserControlPage;
                                        newTab.Header = stackPanel;
                                        tabControlBTC.Items.Add(newTab);
                                        tabControlBTC.SelectedItem = NewUserControlPage;

2 个答案:

答案 0 :(得分:0)

您刚刚在TabItem中添加了一个TabControl。将一个或多个项目添加到TabControl中,以查看样式中的IsMouseOverIsSelected触发器。 TabControl中的单个项目已经被选中。您看不到视觉效果。

答案 1 :(得分:0)

您的TabItem标头不只是Text。 Stackpanel将包含其他控件。 Soi将简单地为ContentControl.Opacity制作动画,如下所示。

<Style x:Key="FlashingHeader" TargetType="TabItem">
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <!--Make The Header -->
                        <ContentControl x:Name="header"  Content="{Binding}" />

                        <!--Make The Background Flash-->
                        <DataTemplate.Triggers>
                            <Trigger Property="Visibility" Value="Visible">
                                <Trigger.EnterActions>
                                    <BeginStoryboard>
                                        <Storyboard>
                                            <DoubleAnimation
                                                Storyboard.TargetName="header"
                                                Storyboard.TargetProperty="(ContentControl.Opacity)"
                                                From="1" To="0" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever" /> 
                                        </Storyboard>
                                    </BeginStoryboard>
                                </Trigger.EnterActions>
                            </Trigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>