WPF简单动画问题 如何通过一个控件的MouseEnter事件触发另外一个控件的动画(比如高度变化),如何解决? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那就在这个控件的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);} 噢,谢谢了!我想的是在XAML中实现,可以吗? <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> 不好意思,没明白,你这样式是给Button的吧,还是只对Button起作用啊~ 我是想这样,一个Image在一个Grid中,通过鼠标进入Image增加Grid的高度,然后鼠标离开Grid的时候,Grid的高度恢复 按照你描述的,给你做了一个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> 可以了,无限感激~!顺便再问一下,可不可以写在资源里,方便多个这种情况使用,并且Height每次是不同的?能这样吗? 给个样本行不?我在资源里写TargetName为Grid名字的时候运行提示不能这样用- -,要怎么弄?Height怎么根据Grid内部控件的高度动态设置呢? 具体要控制的控件的高度和宽度值需要在后台通过代码获得并设置Storyboard的 From 和 To值的。 引用 8 楼 taomanman 的回复:按照你描述的,给你做了一个demoC# code<Window x:Class="WpfApplication1.MainWindow"xmlns="http://sche……[/Quote]给个例子吧~~~~~~~~~~~~~~~~~~~~~~急需 求一个打印效果 C#怎样调用C#写的COM组件 基础框架.NET框架搭建 winform的问题 100大元求解 .net如何调用dll? 在线等呆 要做个基于IE内核的浏览器,希望你给建议! 子窗体退出后关闭父窗体? button与tab的问题 为何这样连接数据库不行啊? 请用过winform的水晶报表的各位高手,看看我的是哪里出了错误,谢谢,在线等。 多态性到底有什么好处,能否举个例子? C# propertygrid中,做一个类似Location的属性,但是有点问题,请大家看看。
假设你的控件是一个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);
}
噢,谢谢了!我想的是在XAML中实现,可以吗?
<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>
不好意思,没明白,你这样式是给Button的吧,还是只对Button起作用啊~
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>
可以了,无限感激~!顺便再问一下,可不可以写在资源里,方便多个这种情况使用,并且Height每次是不同的?能这样吗?
按照你描述的,给你做了一个demoC# code<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://sche……
[/Quote]给个例子吧~~~~~~~~~~~~~~~~~~~~~~急需