当我的控件旋转发生变化时,如何获得通知
How do I get an notification when Rotation of my control has Changed
UserControl
class 为我提供了 SizeChanged
这样的活动。所以当尺寸改变时我可以在我的控件上做任何操作:
XAML:
<UserControl x:Class="TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Loaded="UserControl_Loaded" SizeChanged="UserControl_SizeChanged">
代码隐藏:
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
// do anything
}
现在我想知道我的控件的旋转何时也发生了变化。然而, UserControl
class 并没有给我一个 RotationChanged-Event.
我想要跟踪旋转的原因是我的用户控件中有一些我想旋转回来的元素,所以它们总是与应用程序的水平轴/方向对齐。 (其他 parent 元素不会旋转,所以我不必关心这些。)
这是一张图片,说明了我想要更多归档的内容:
黑框是用户控件,而红框是我的用户控件中的UIElement
。默认状态在最上面。
当用户旋转我的控件时(底部示例),我想 back-rotate 我控件中的元素。如果没有 RotationChanged
事件,我还能怎么做?
编辑:
对于 Sheridan 的给定解决方案,我想提及该事件必须在我的用户控件的 Loaded 事件中订阅。在 Changed 事件中,我可以简单地转换为 RotateTransform
并检查是否为 null。在初始轮换中,我必须尝试在 Loaded 事件中将 this.RenderTransform 转换为 RotateTransform
或 TransformGroup
(children 我可以循环)。通过这种方式,我得到了我的角度。
但是,如果你像我一样尝试归档,还有一个问题,因为设计器将覆盖你的 RenderTransform
节点,因此 RenderTransform.Changed
事件将会丢失。我在 Whosebug 上将其作为自己的问题分开:
UserControl
class 上没有 RotationChanged
事件,因为 class 不执行旋转。而在 WPF 中,旋转是由 RotationTransform
Class. It is on that class that you can find the Freezable.Changed
event.
执行的
RotationTransform transform = new RotationTransform();
transform.Changed += OnRotationChanged;
...
public void OnRotationChanged(object sender, EventArgs e)
{
...
}
UserControl
class 为我提供了 SizeChanged
这样的活动。所以当尺寸改变时我可以在我的控件上做任何操作:
XAML:
<UserControl x:Class="TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Loaded="UserControl_Loaded" SizeChanged="UserControl_SizeChanged">
代码隐藏:
private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
// do anything
}
现在我想知道我的控件的旋转何时也发生了变化。然而, UserControl
class 并没有给我一个 RotationChanged-Event.
我想要跟踪旋转的原因是我的用户控件中有一些我想旋转回来的元素,所以它们总是与应用程序的水平轴/方向对齐。 (其他 parent 元素不会旋转,所以我不必关心这些。)
这是一张图片,说明了我想要更多归档的内容:
黑框是用户控件,而红框是我的用户控件中的UIElement
。默认状态在最上面。
当用户旋转我的控件时(底部示例),我想 back-rotate 我控件中的元素。如果没有 RotationChanged
事件,我还能怎么做?
编辑:
对于 Sheridan 的给定解决方案,我想提及该事件必须在我的用户控件的 Loaded 事件中订阅。在 Changed 事件中,我可以简单地转换为 RotateTransform
并检查是否为 null。在初始轮换中,我必须尝试在 Loaded 事件中将 this.RenderTransform 转换为 RotateTransform
或 TransformGroup
(children 我可以循环)。通过这种方式,我得到了我的角度。
但是,如果你像我一样尝试归档,还有一个问题,因为设计器将覆盖你的 RenderTransform
节点,因此 RenderTransform.Changed
事件将会丢失。我在 Whosebug 上将其作为自己的问题分开:
UserControl
class 上没有 RotationChanged
事件,因为 class 不执行旋转。而在 WPF 中,旋转是由 RotationTransform
Class. It is on that class that you can find the Freezable.Changed
event.
RotationTransform transform = new RotationTransform();
transform.Changed += OnRotationChanged;
...
public void OnRotationChanged(object sender, EventArgs e)
{
...
}