如何通过一个控件的MouseEnter事件触发另外一个控件的动画(比如高度变化),如何解决?

解决方案 »

  1.   

    那就在这个控件的MouseEnter事件中增加一些代码,如下:
    假设你的控件是一个Button,要控制的是一个Rectangle
    <Rectangle Name="rect" Width="100" Height="100"/>
    <Button x:Name="btn" MouseEnter="btn_MouseEnter" Width="50" Height="50" Content="移动我试试"/>protected void btn_MouseEnter(object sender, MouseEventArgs e)
    {
         Storyboard sb = new Storyboard();
         DoubleAnimation da= new DoubleAnimation();
         da.From = 100;
         da.To = 300;
         da.Duration = new Duration(TimeSpan.FromSeconds(1));
         sb.SetTargetName(da, rect.Name);
         sb.SetTargetProperty(da, new PropertyPath(Rectangle.WidthProperty));
         sb.Children.Add(da);
          sb.Begin(this);
    }
      

  2.   


    噢,谢谢了!我想的是在XAML中实现,可以吗?
      

  3.   


    <Window x:Class="WPF.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WPF"       
              xmlns:sys="clr-namespace:System;assembly=mscorlib"
              Title="MainWindow" Height="250" Width="450">
          <Window.Resources>
              <Style TargetType="Button">
                  <Setter Property="Margin" Value="10"></Setter>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="MouseEnter">
                          <BeginStoryboard>
                              <Storyboard>                            
                                  <DoubleAnimation To="20" Duration="0:0:2" Storyboard.TargetProperty="Width"></DoubleAnimation>
                                  <DoubleAnimation To="70" Duration="0:0:2" Storyboard.TargetProperty="Height"></DoubleAnimation>
                              </Storyboard>
                        </BeginStoryboard>
                      </EventTrigger>
                  </Style.Triggers>
              </Style>
        </Window.Resources>
          <StackPanel x:Name="stackPanel">
              <Button Width="100" Height="20">移入我试试看</Button>
          </StackPanel>
    </Window> 
      

  4.   


    不好意思,没明白,你这样式是给Button的吧,还是只对Button起作用啊~
      

  5.   

    我是想这样,一个Image在一个Grid中,通过鼠标进入Image增加Grid的高度,然后鼠标离开Grid的时候,Grid的高度恢复
      

  6.   

    按照你描述的,给你做了一个demo<Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid Height="200" HorizontalAlignment="Left" Margin="79,60,0,0" Background="Green" x:Name="grid1" VerticalAlignment="Top" Width="400">
                <Image Height="150" HorizontalAlignment="Left" Margin="83,16,0,0" x:Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="/WpfApplication1;component/Images/azsy.png">
                    <Image.Triggers>
                        <EventTrigger RoutedEvent="MouseEnter">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="grid1" Storyboard.TargetProperty="Height" From="200" To="250" Duration="0:0:5"></DoubleAnimation>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="MouseLeave">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="grid1" Storyboard.TargetProperty="Height" From="250" To="200" Duration="0:0:5"></DoubleAnimation>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </Image.Triggers>
                </Image>
            </Grid>
        </Grid>
    </Window>
      

  7.   


    可以了,无限感激~!顺便再问一下,可不可以写在资源里,方便多个这种情况使用,并且Height每次是不同的?能这样吗?
      

  8.   

    给个样本行不?我在资源里写TargetName为Grid名字的时候运行提示不能这样用- -,要怎么弄?Height怎么根据Grid内部控件的高度动态设置呢?
      

  9.   

    具体要控制的控件的高度和宽度值需要在后台通过代码获得并设置Storyboard的 From 和 To值的。
      

  10.   

    引用 8 楼 taomanman 的回复:
    按照你描述的,给你做了一个demoC# code<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://sche……
    [/Quote]给个例子吧~~~~~~~~~~~~~~~~~~~~~~急需