我有一个自定义UserControl,其位置绑定到ViewModel中的参数。
<Canvas Name="CursorCanvas">
<ctrl:HandCursor x:Name="RightHandCursor" State="{Binding RightHandState}" Visibility="{Binding IsHandTracking, Converter={StaticResource BoolToVisibilityConverter}}" Canvas.Top="{Binding RightHandY}" Canvas.Left="{Binding RightHandX}" />
</Canvas>
在此控件内部,我正在执行HitTest,以确定其下方的内容。目前,此HitTest连接到DispatcherTimer,间隔为1秒。我宁愿告诉控件在Canvas上移动时执行HitTest,但是我无法找到如何捕获正在移动的UserControl中的Canvas位置更新。
如何在上面定义的“HandCursor”UserControl的代码隐藏中捕获Canvas.Top/Left移动更新?
非常感谢!
答案 0 :(得分:3)
您可以通过覆盖Canvas.Left
和Canvas.Top
附加属性的属性元数据来添加PropertyChangedCallback:
static HandCursor()
{
Canvas.LeftProperty.OverrideMetadata(
typeof(HandCursor),
new FrameworkPropertyMetadata(
(o, e) => ((HandCursor)o).LeftPropertyChanged((double)e.NewValue)));
Canvas.TopProperty.OverrideMetadata(
typeof(HandCursor),
new FrameworkPropertyMetadata(
(o, e) => ((HandCursor)o).TopPropertyChanged((double)e.NewValue)));
}
private void LeftPropertyChanged(double left)
{
...
}
private void TopPropertyChanged(double top)
{
...
}