简体   繁体   English

c# WPF .NET 核心 - 如何在父母之外进行动画控制?

[英]c# WPF .NET Core - How animate control outside parents?

I've got a tabbed UI (WPF) like this:我有一个像这样的选项卡式 UI (WPF):

 ___   ___
/ A \_/_B_\_____
|  ___________  |
| |  (List)   | |
| |  ______   | |
| | | Data |  | |
| | |______|  | |
| |  ______   | |
| | | Data |  | |
| | |______|  | |
| |  ______   | |
| | | Data |  | |
| | |______|  | |
| |___________| |
|_______________|

In each "Data" section there is data (duh) but also a button that causes the state of the data to change so that it disappears from tab A and appears instead in tab B. For better or worse, this is the design the client wants, so unfortunately restructuring is not an option.在每个“数据”部分中都有数据(duh),但也有一个按钮导致数据的 state 发生变化,使其从选项卡 A 中消失并出现在选项卡 B 中。无论好坏,这是客户端的设计想要,所以不幸的是,重组不是一种选择。

So right now when the user hits the button in a Data section, the Data just disappears and the user has to "know" that it's now moved to tab B. To show the user what is happening, I was imagining animating the data block when the user hits the button so that it shrinks and moves up to the tab labeled "B" and maybe even have the tab pulse when it gets there or something.所以现在当用户点击数据部分中的按钮时,数据就消失了,用户必须“知道”它现在已经移动到选项卡 B。为了向用户展示正在发生的事情,我想象在动画数据块时用户点击按钮,使其缩小并向上移动到标有“B”的选项卡,甚至可能在它到达那里时有选项卡脉冲或其他东西。

I've got the animation code working, it was a little tricky to convince a Storyboard to animate position and size at the same time, but thanks to a previous StackOverflow answer I was able to write this and it works:我已经让 animation 代码正常工作,说服 Storyboard 为 position 和大小同时设置动画有点棘手,但感谢之前的 StackOverflow 答案,它能够工作:

TransformGroup group = new TransformGroup();
TranslateTransform trans = new TranslateTransform();
group.Children.Add(trans);
ScaleTransform scale = new ScaleTransform(1, 1);
group.Children.Add(scale);
RenderTransform = group;

var storyBoard = new Storyboard();

var yMove = new DoubleAnimation(0, newY, new Duration(new TimeSpan(0, 0, 3)));
Storyboard.SetTarget(yMove, this);
Storyboard.SetTargetProperty(yMove, new PropertyPath("RenderTransform.Children[0].Y"));
storyBoard.Children.Add(yMove);

var xMove = new DoubleAnimation(0, newX, new Duration(new TimeSpan(0, 0, 3)));
Storyboard.SetTarget(xMove, this);
Storyboard.SetTargetProperty(xMove, new PropertyPath("RenderTransform.Children[0].X"));
storyBoard.Children.Add(xMove);

var width = new DoubleAnimation(1, scaledWidth, new Duration(new TimeSpan(0, 0, 3)));
Storyboard.SetTarget(width, this);
Storyboard.SetTargetProperty(width, new PropertyPath("RenderTransform.Children[1].ScaleX"));
storyBoard.Children.Add(width);

var height = new DoubleAnimation(1, scaledHeight, new Duration(new TimeSpan(0, 0, 3)));
Storyboard.SetTarget(height, this);
Storyboard.SetTargetProperty(height, new PropertyPath("RenderTransform.Children[1].ScaleY"));
storyBoard.Children.Add(height);

storyBoard.Begin(this);

The problem is that as soon as a Data control tries to move outside of its parent, it gets clipped.问题在于,一旦 Data 控件试图移出其父控件,它就会被剪裁。 I tried setting ClipToBounds to false but it didn't help.我尝试将 ClipToBounds 设置为 false,但没有帮助。

Is there a way to "set my control free" so it can move around the screen without getting clipped?有没有办法“让我的控制自由”,这样它就可以在屏幕上移动而不会被剪裁?

You could try to use an adorner layer, there you should be free to move the objects wherever you want.你可以尝试使用装饰层,在那里你应该可以自由地将对象移动到任何你想要的地方。 You can create the same the control in the adorner layer, collapse you actual control and then animate the control in the adorner layer.您可以在装饰层中创建相同的控件,折叠实际控件,然后在装饰层中为控件设置动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM