将鼠标悬停在效果上并在IsSelected(UserControl,WPF)

时间:2019-01-11 14:17:43

标签: c# wpf animation user-controls mouseevent

我想在鼠标进入时悬停效果,在鼠标移开时恢复透明。但是在UserControl IsSelected(viewmodel属性)时禁用此动画。

我有一些有效的代码,但不是我想要的,并且有一些问题。当鼠标进入和开始播放动画时,用户控件将白色背景和快速动画设置为我需要的颜色。当IsSelected为true时,此动画也播放我不需要。

<UserControl.Resources>
        <Style x:Key="ContentStyle" TargetType="{x:Type ContentControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContentControl}">

                        <!-- Content -->
                        <Border x:Name="background" Background="#2b2d33">

                            <!-- Click to open message -->
                            <Border.InputBindings>
                                <MouseBinding MouseAction="LeftClick" Command="{Binding GoToChat}" />
                            </Border.InputBindings>

                            <Grid x:Name="container" Background="Transparent">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>


                                <Border Grid.Column="0" 
                                    Padding="8 15"
                                    >

                                    <Border Height="40" Width="40" CornerRadius="50,50,50,50">
                                        <Border.Background>
                                            <ImageBrush ImageSource="{Binding Photo}"/>
                                        </Border.Background>
                                    </Border>
                                </Border>

                                <!-- Main content -->
                                <Border Grid.Column="1" Padding="0 0 15 0">
                                    <StackPanel VerticalAlignment="Center">
                                        <!-- Name-->
                                        <TextBlock Text="{Binding Name}"
                                                Padding="0 0 0 2"
                                                Foreground="{StaticResource WordBlueBrush}"
                                                TextTrimming="CharacterEllipsis"
                                                FontFamily="{StaticResource Amsterdam}"
                                                />
                                        <!-- Message -->
                                        <TextBlock Text="{Binding Message}"
                                               Padding="0 2 0 0"
                                               Foreground="{StaticResource ForegroundDarkBrush}"
                                               TextTrimming="CharacterEllipsis"
                                               FontFamily="{StaticResource Amsterdam}"
                                               />

                                    </StackPanel>
                                </Border>

                            </Grid>
                        </Border>

                        <!-- Hover effect -->
                        <ControlTemplate.Triggers>

                            <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                <Setter Property="Background" TargetName="background" Value="#40444c"/>
                            </DataTrigger>

                            <EventTrigger RoutedEvent="MouseEnter">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation To="#33363d" Duration="0:0:0.3" Storyboard.TargetName="container" Storyboard.TargetProperty="Background.Color" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                            <EventTrigger RoutedEvent="MouseLeave">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation To="Transparent" Duration="0:0:0.3" Storyboard.TargetName="container" Storyboard.TargetProperty="Background.Color" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </ControlTemplate.Triggers>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

    <ContentControl Style="{StaticResource ContentStyle}" />
</UserControl>

0 个答案:

没有答案