Mark elements in WPF
I would like to group elements in my GUI. They are all in the same canvas, so they are not grouped visually. Some of them I want to move and some of them I want to shrink.
As I implement the animation in C# and not XAML I would like to know, if it is possible to mark the elements with a boolean or something similar in order to check its value when choosing the appropriate animati开发者_开发问答on.
Since you are doing the animation in code-behind you can use pretty much any way you want to attach this extra boolean to the objects.
As a simple approach, you can use FrameworkElement.Tag
on each of the elements to put your extra data in. But there's a million other possibilities as well.
If you have to associate extra data with the controls to flag them for animations, I would suggest using an attached property.
In general I would suggest evaluating the objects themselves to determine how to animate them, but in some cases meta data is required. Attached properties fits this need perfectly.
Define a static class:
public static class AnimationType
{
public static bool GetShrink(DependencyObject obj)
{
return (bool)obj.GetValue(ShrinkProperty);
}
public static void SetShrink(DependencyObject obj, bool value)
{
obj.SetValue(ShrinkProperty, value);
}
public static readonly DependencyProperty ShrinkProperty =
DependencyProperty.RegisterAttached("Shrink", typeof(bool), typeof(AnchoredBlock), new UIPropertyMetadata(false));
}
When you go to animate, call AnimationType.GetShrink(myControl);
and test the boolean return to see how to animate the control.
精彩评论