如何通过动画Animation修改VisualBrush的Visual绑定? 本帖最后由 xbycszozbsgw 于 2012-07-05 09:39:46 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 WPF目前不允许这样的用法,动画类的数据必须是可以freeze的 不行吗?但我在网上看到有人用下面的方法,修改Image的Source的,这样的方法,应该是很接近了吧?但应该怎样把这个例子,套用到我的需求?<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(Image.Source)"> <DiscreteObjectKeyFrame KeyTime="00:00:00.7000000"> <DiscreteObjectKeyFrame.Value> <BitmapImage UriSource="check_24.png" /> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> 如果我把代码修改成下面的呢?有没有办法使用动画?Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Visual)"或者Storyboard.TargetProperty="(Rectangle.Fill).(VisualBrush.Visual).(Binding.ElementName)"<Rectangle Name="rectangle1" Height="100" Width="100"> <Rectangle.Fill> <VisualBrush> <VisualBrush.Visual> <Binding ElementName="button1"></Binding> </VisualBrush.Visual> </VisualBrush> </Rectangle.Fill></Rectangle> 将画刷放在资源字典里,需要用的时候再绑定button的样式 个人能想到的方法:把Fill绑定到一个自定义属性上,或者是Tag上,自定义一个Converter来解析到具体的画刷。动画板直接改变属性值以实现改变画刷的功能。 新建一个类文件放转换器代码: public class BrushConverter : IValueConverter { #region IValueConverter 成员 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) value = "vb1"; return App.Current.MainWindow.TryFindResource(value.ToString()); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion }在Windows.Resource里写资源 <VisualBrush x:Key="vb1" Visual="{Binding ElementName=btn1}"/> <VisualBrush x:Key="vb2" Visual="{Binding ElementName=btn2}"/> <Storyboard x:Key="OnClick1"> <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Tag)" Storyboard.TargetName="Window"> <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="vb2"/> </ObjectAnimationUsingKeyFrames> </Storyboard>绑定画刷到Tag(比如Window的Tag)<Rectangle Fill="{Binding Tag, Converter={StaticResource BrushConverter}, ElementName=Window, FallbackValue=vb1}" Stroke="Black"/>大概就是这样 忘了个<local:BrushConverter x:Key="BrushConverter"/>local是xmlns:local="clr-namespace:项目的命名空间" 我这可以运行,主窗口是Window 我知道原因了,因为在程序运行之前,App是还没有被实例化的,因此,界面预览会出错。解决方法是,添加一个try:public class BrushConverter : IValueConverter { #region IValueConverter 成员 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) value = "vb1"; try { return App.Current.MainWindow.TryFindResource(value.ToString()); } catch (Exception e) {} return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } #endregion } 智能设备程序连接access C# 抛异常的问题 C# 查询MySQL为乱码 谁有关于 线程进程 这方面知识比较具体的资料啊 DATAGRIDVIEW合并单元格问题(winform) 请教一个.net入门问题,C/S架构 谁能帮忙把这段C++的代码改写成C#的 所有代码都需要注释么? 小弟很急!还请各位高手帮忙!! 菜鸟送分!!在SQL连接串中有一个这样的字符串,是什么意思呀? C#不同应用程序之间的文本复制和粘贴 winform中在MDI主窗体显示多个子窗体
但我在网上看到有人用下面的方法,修改Image的Source的,这样的方法,应该是很接近了吧?但应该怎样把这个例子,套用到我的需求?
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="image" Storyboard.TargetProperty="(Image.Source)">
<DiscreteObjectKeyFrame KeyTime="00:00:00.7000000">
<DiscreteObjectKeyFrame.Value>
<BitmapImage UriSource="check_24.png" />
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<Rectangle.Fill>
<VisualBrush>
<VisualBrush.Visual>
<Binding ElementName="button1"></Binding>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
把Fill绑定到一个自定义属性上,或者是Tag上,自定义一个Converter来解析到具体的画刷。动画板直接改变属性值以实现改变画刷的功能。
public class BrushConverter : IValueConverter
{ #region IValueConverter 成员 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) value = "vb1";
return App.Current.MainWindow.TryFindResource(value.ToString());
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
} #endregion
}
在Windows.Resource里写资源
<VisualBrush x:Key="vb1" Visual="{Binding ElementName=btn1}"/>
<VisualBrush x:Key="vb2" Visual="{Binding ElementName=btn2}"/>
<Storyboard x:Key="OnClick1">
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Tag)" Storyboard.TargetName="Window">
<DiscreteObjectKeyFrame KeyTime="0:0:1" Value="vb2"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
绑定画刷到Tag(比如Window的Tag)
<Rectangle Fill="{Binding Tag, Converter={StaticResource BrushConverter}, ElementName=Window, FallbackValue=vb1}" Stroke="Black"/>
大概就是这样
<local:BrushConverter x:Key="BrushConverter"/>
local是
xmlns:local="clr-namespace:项目的命名空间"
解决方法是,添加一个try:
public class BrushConverter : IValueConverter
{ #region IValueConverter 成员 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) value = "vb1";
try
{
return App.Current.MainWindow.TryFindResource(value.ToString());
}
catch (Exception e)
{}
return null;
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
} #endregion
}