简体   繁体   English

对其他控件上方的控件高度进行动画处理

[英]Animate control height over the top of other controls

I have a custom control ( CustomButton.cs ) that grows in height (and width) when you hover over it (Screenshot 1). 我有一个自定义控件( CustomButton.cs ),当您将鼠标悬停在其上时,它的高度(和宽度)会增加(屏幕截图1)。 Currently when the control grows, the rest of the layout shrinks to give the control space to grow (Screenshot 2). 当前,当控件增长时,其余布局会缩小以提供控件空间来增长(屏幕截图2)。 I would like the layout to remain static and the control to grow over the other controls and overlap instead but I am unable to accomplish this. 我希望布局保持不变,并且希望控件在其他控件上扩展并重叠,但是我无法实现这一点。

I have tried using ClipToBounds="False" and also tried to wrap my control in a <Canvas> both of which did not allow my control to overlap the other controls. 我尝试使用ClipToBounds="False" ,还尝试将控件包装在<Canvas> ,这两个都不允许我的控件与其他控件重叠。 I also attempted to use a <Popup> but couldn't get the location correct, it kept appearing off-screen. 我也尝试使用<Popup>但无法正确获取位置,它一直显示在屏幕外。

Does anyone know how to get this working? 有谁知道如何使它工作?

Custom control before hover (Screenshot 1) 悬停前的自定义控件(屏幕截图1)

悬停前的自定义控件(屏幕截图1)

Custom control while hovering (Screenshot 2) 悬停时的自定义控件(屏幕截图2)

悬停后的自定义控件(屏幕截图2)

MainWindow.xaml MainWindow.xaml

<Window x:Class="Tests.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Tests"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Grid Margin="10">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="10"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Border Grid.Row="0" Background="Beige" BorderBrush="Black" BorderThickness="1" ></Border>
    <StackPanel Grid.Row="2" Orientation="Horizontal">
        <local:CustomButton Margin="0,0,10,0">Text1</local:CustomButton>
        <local:CustomButton>Text2</local:CustomButton>
    </StackPanel>
</Grid>

CustomButton.cs CustomButton.cs

public class CustomButton : Button
{
    static CustomButton()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
    }

    public CustomButton()
    {
        this.Loaded += OnLoaded;
    }

    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        this.Width = this.ActualWidth;
        this.Height = this.ActualHeight;
    }
}

Generic.xaml Generic.xaml

<Style TargetType="{x:Type local:CustomButton}">
    <Setter Property="Background" Value="LightBlue"></Setter>
    <Setter Property="BorderBrush" Value="Black"></Setter>
    <Setter Property="BorderThickness" Value="1"></Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomButton}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBlock Text="{TemplateBinding Content}" Padding="5" HorizontalAlignment="Center"></TextBlock>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <!-- Button increase Width/Height animation -->
                                    <DoubleAnimation Storyboard.TargetProperty="Height" By="20" Duration="0:0:0.1" />
                                    <DoubleAnimation Storyboard.TargetProperty="Width" By="50" Duration="0:0:0.1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <!-- Button decrease Width/Height animation -->
                                    <DoubleAnimation Storyboard.TargetProperty="Height" By="-20" Duration="0:0:0.2" />
                                    <DoubleAnimation Storyboard.TargetProperty="Width" By="-50" Duration="0:0:0.2" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Would doing something like: 会做类似的事情:

<Grid Margin="10">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="10"></RowDefinition>
        <RowDefinition Height="50"></RowDefinition>
    </Grid.RowDefinitions>
    <Border Grid.Row="0" Background="Beige" BorderBrush="Black" BorderThickness="1" ></Border>
    <StackPanel Grid.Row="0" Grid.RowSpan="3" Orientation="Horizontal" VerticalAlignment="Bottom" >
        <Local:CustomButton Margin="0,0,10,0">Text1</Button>
        <Local:CustomButton>Text2</Button>
    </StackPanel>
</Grid>

Work for you? 为你工作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM