画布项目的项目的Storyboard动画,位于代码后面. [英] Storyboard Animation of Canvas item's item in code behind.

查看:73
本文介绍了画布项目的项目的Storyboard动画,位于代码后面.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个画布,其中包含Items(View类对象-也具有ViewModel).

I have a canvas which has Items(View class objects - that has a ViewModel also) .

View类的内容是这样的...

The View class content is like this...

<TextBlock  Text="{Binding DataContext.Txt,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContentControl}}}" />
                               

                                <ListBox Grid.Column="1" Grid.RowSpan="2" ItemsSource="{Binding DataContext.ColorCollection}">                                   
                                    <ListBox.Resources>
                                        <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}" >                                           
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="ListBoxItem">
                                                        <Border BorderBrush="Silver" BorderThickness="0.45">
                                                            <TextBlock Height="8" Width="10" 
                                                                   Background="{Binding Path=FillColor}"/>
                                                        </Border>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </ListBox.Resources>
                                </ListBox>

我想使用后面代码中的情节提要动画对Canvas每个项目的项目(即ListBoxItem)进行动画处理.

I would like to animate Canvas each Item's item (Ie, ListBoxItem) using storyboard animation in code behind.

我引用了此链接: http://social.msdn.microsoft.com/Forums/vstudio/zh-CN/c4a35fb6-0672-4895-91a1-06dcc5b9d662/assign-storyboard-in-code-behind?forum=wpf

I refered to this link:   http://social.msdn.microsoft.com/Forums/vstudio/en-US/c4a35fb6-0672-4895-91a1-06dcc5b9d662/assign-storyboard-in-code-behind?forum=wpf

但是,它一次只能动画一个项目.

BUT, it allows to animate only one item at a time.

我也尝试过->

Also, I tried doing->

 //ColorAnimationUsingKeyFrames cukf = new ColorAnimationUsingKeyFrames();
//Storyboard.SetTargetProperty(cukf,new PropertyPath(ListBoxItem.BackgroundProperty));

 //ColorAnimationUsingKeyFrames cukf = new ColorAnimationUsingKeyFrames();
//Storyboard.SetTargetProperty(cukf, new PropertyPath(ListBoxItem.BackgroundProperty));

但是...它给出错误为:

but... it gives error as:

System.Windows.Media.Animation.ColorAnimationUsingKeyFrames的动画对象不能用于对属性"Foreground"进行动画处理,因为它的类型不兼容"System.Windows.Media.Brush".

System.Windows.Media.Animation.ColorAnimationUsingKeyFrames' animation object cannot be used to animate property 'Foreground' because it is of incompatible type 'System.Windows.Media.Brush'.

-----------------------------------

-------------------------------

在这里...必须动画的画布项目(及其项目索引).将在运行时给出(当然是在后面的代码中),以便为颜色变化设置动画.

HERE... the  Canvas item ,that has to be animated, ( & its item index )  will be given at runtime (in code behind of course), in order to animate for color change.

请提出建议.谢谢.

推荐答案

乔,

因为列表框的前景类型为 System.Windows.Media.Brush .我们无法将ColorAnimationUsingKeyFrames类型分配给ListBox的前景.由于无法转换为画笔类型.我提到了这个 线程.并在此处引用了有用的信息.

Because the ListBox’s Foreground type is System.Windows.Media.Brush. We cannot assign the ColorAnimationUsingKeyFrames type to ListBox’s Foreground. Since it cannot convert to Brush Type. I referred to this thread. And quoted the useful information here.

<已复制>

<Copied>

您需要扩展Panel类,并将其用作ListBox.ItemsPanel属性的ItemsPanelTemplate.这真的很容易.只有两种方法可以覆盖.一种用于测量ListBox中的项目,另一种用于排列它们.这是微软 关于该主题的文章.

You would need to extend the Panel class and use it as the ItemsPanelTemplate of your ListBox.ItemsPanel property. It's really easy... there's just two methods to override; one to measure the items in the ListBox and one to arrange them. Here is a Microsoft article on the subject.

这里可能是 更有用的文章,其中显示了如何对项目进行动画处理.为了达到效果,您只需将位置动画上的from值设置为相同位置,就在每个项目的可见区域之外.例如,使用起始位置0 finalSize.Height表示每个项目将从ListBox的左下角滑至其位置.您可以使用新的动画.

Here is perhaps a more useful article that shows how to animate the items. For your effect, you would simply set the from value on your position animations to be the same location just off the viewable area for each of your items. For example, using a from position of 0, finalSize.Height would mean that each item would slide to its position from the bottom left corner of the ListBox. You could use your new animated.

</已复制>

</Copied>

这是一个有关在ListBox控件中对选定项目进行动画处理的示例.

Here is a sample about animate the selected item in ListBox control.

#SlidingListBox –在WPF中对ListBoxItem进行动画处理

# SlidingListBox – Animating ListBoxItems in WPF

http://www.codeproject.com/Articles/16045/SlidingListBox-Animating -ListBoxItems-in-WPF

此外,这是一个类似的主题,我会向您推荐.

Also, here is a similar thread, I would recommend to you.

玩得开心!

此致


这篇关于画布项目的项目的Storyboard动画,位于代码后面.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆