在MouseOver上更改DockPanel可见性

时间:2018-01-05 03:11:45

标签: c# wpf

我试图改变Visibility.Visible到Visibility.Collapsed,反之亦然,当Background不是黑色时。 如果我使用Opacity而不是Visibility,代码可以正常工作。

我做错了什么?

    <Grid Name="myGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="36" />
        <ColumnDefinition Width="36" />
        <ColumnDefinition Width="36" />
        <ColumnDefinition Width="100*" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="200*" />
        <RowDefinition Height="40" />
    </Grid.RowDefinitions>

    <DockPanel Name="BottomPanel" Style="{StaticResource Panel}" Background="Black" Visibility="Visible" Grid.Row="2" Grid.ColumnSpan="4"  Panel.ZIndex="100">
        <Button Name="btn" Grid.Column="0" Grid.Row="1" Content="BTN1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="36" Click="Button_Click" RenderTransformOrigin="-0.557,14.529"/>
        <Button Name="btn2" Grid.Column="1" Grid.Row="1" Content="BTN2" HorizontalAlignment="Left" VerticalAlignment="Top" Width="36" Click="btn2_Click"/>
        <Button Name="btn3" Grid.Column="2" Grid.Row="1" Content="BTN3" BorderThickness="0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="36" Click="btn3_Click"/>          
    </DockPanel>  
</Grid>


<Style x:Key="Panel" TargetType="DockPanel">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Visibility" Value="Visible" />
        </Trigger>

        <Trigger Property="IsMouseOver" Value="False">
            <Setter Property="Visibility" Value="Collapsed" />
        </Trigger>

        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="Background" Value="Black"/>
            </MultiTrigger.Conditions>
            <MultiTrigger.Setters>
                <Setter Property="Visibility" Value="Visible" />
            </MultiTrigger.Setters>
        </MultiTrigger>
    </Style.Triggers>
</Style>

1 个答案:

答案 0 :(得分:4)

每当控件被隐藏或折叠时,它将停止处理任何输入,因此您不会触发任何触发但您可以使用Opacity属性来实现行为

        <Style x:Key="Panel" TargetType="DockPanel">
            <Setter Property="Opacity" Value="0"/>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="Background" Value="Black" />
                        <Condition Property="IsMouseOver" Value="True"/>
                    </MultiTrigger.Conditions>
                    <MultiTrigger.Setters>
                        <Setter Property="Opacity" Value="1" />
                    </MultiTrigger.Setters>
                </MultiTrigger>
            </Style.Triggers>
        </Style>