扩展按钮

时间:2013-09-05 14:00:16

标签: c# wpf

我创建了一个带有一堆按钮的窗口。这些按钮是我创建的用户控件。

这就是我的窗口现在的样子,它包含11个用户控件,但窗口上的用户控件数是动态的:

enter image description here

当用户用鼠标悬停在按钮上时,我希望控件扩展20个像素 问题是控件会扩展,但其他控件会保留在原位,因此它们会重叠。

这是我的UserControl

<UserControl
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"
mc:Ignorable="d"
x:Class="UserControlSolution.UserControlButton"
x:Name="UserControl"
Height="50">
<UserControl.Resources>
    <Storyboard x:Key="OnMouseEnter1">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="rectangle">
            <EasingDoubleKeyFrame KeyTime="0" Value="1.5"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="rectangle">
            <EasingDoubleKeyFrame KeyTime="0" Value="12.5"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>
<UserControl.Triggers>
    <EventTrigger RoutedEvent="Mouse.MouseEnter">
        <BeginStoryboard Storyboard="{StaticResource OnMouseEnter1}"/>
    </EventTrigger>
</UserControl.Triggers>

<Grid x:Name="LayoutRoot" Height="50">
    <Rectangle x:Name="rectangle" RenderTransformOrigin="0.5,0.5">
        <Rectangle.Stroke>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF5C5C5C" Offset="0"/>
                <GradientStop Color="#FF4E4E4E" Offset="1"/>
            </LinearGradientBrush>
        </Rectangle.Stroke>
        <Rectangle.Fill>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF626262" Offset="0"/>
                <GradientStop Color="#FF3B3B3B" Offset="0.987"/>
            </LinearGradientBrush>
        </Rectangle.Fill>
    </Rectangle>
    <TextBlock x:Name="NameLabel" FontFamily="Levenim MT" FontSize="16" Foreground="#FFE5E5E5" Height="34" Width="230" Text="Onthaal Telefoon" VerticalAlignment="Top" TextAlignment="Center" HorizontalAlignment="Center" Margin="0,10,0,0"/>
</Grid>

这是我的窗口

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:UserControlSolution" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="UserControlSolution.MainWindow"
    Title="MainWindow" Height="836" Width="262" Background="#FF2B2B2B" BorderBrush="{DynamicResource Border}" Loaded="Window_Loaded" >

<Window.Resources>

    <LinearGradientBrush x:Key="Border" EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
        <GradientStop Color="#FF6C6C6C" Offset="0.009"/>
        <GradientStop Color="#FFA1A1A1" Offset="1"/>
    </LinearGradientBrush>

</Window.Resources>

<StackPanel Margin="0,10,0,0">
    <local:UserControlButton x:Name="Robby" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Erwin" HorizontalAlignment="Center" Height="50" VerticalAlignment="Top" Width="228" Background="Black" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Laurens" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top"  Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Kevin" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Liesbeth" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Jack" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Filip" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Stefaan" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Sami" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Jurgen" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
    <local:UserControlButton x:Name="Thomas" HorizontalAlignment="Center" Width="228" Background="Black" Height="50" VerticalAlignment="Top" Margin="0,0,0,10"/>
</StackPanel>

如何将扩展控件下的控件向下移动,使它们不重叠? 或者什么是更好的解决方案?

1 个答案:

答案 0 :(得分:1)

您必须更改LayoutTransform而不是RenderTransform才能影响其他元素的布局

相关问题