MeasureOverride 和 ArrangeOverride 未被调用
我正在使用 WPFExtensions 中的 ZoomControl。源代码位于此处。
我已经使用 OnRender 方法添加了装饰器:
protected override void OnRender(DrawingContext drawingContext)
{
drawingContext.DrawEllipse(
new SolidColorBrush(Colors.CornflowerBlue),
null,
new Point(1292, 100),
100, 100);
}
使用 ZoomControl xaml:
<Controls:ZoomControl x:Name="zoomControl1" Background="AliceBlue">
<Canvas Width="{Binding Path=SModel.Width}" Height="{Binding Path=SModel.Height}">
<Image HorizontalAlignment="Center"
Name="image1" Stretch="Fill"
VerticalAlignment="Center" Source="/Z;component/Images/00000006.jpg"
Margin="0,0,0,0" Canvas.Left="1"
Width="{Binding Path=SModel.Width}" Height="{Binding Path=SModel.Height}">
<Image.LayoutTransform>
<RotateTransform Angle="{Binding Path=SModel.Angle}" />
</Image.LayoutTransform>
</Image>
</Canvas>
</Controls:ZoomControl>
应用装饰器:
public Window1()
{
InitializeComponent();
image1.Loaded += loaded;
}
private void loaded(object sender, RoutedEventArgs e)
{
var adorner = new SplitAdorner(image1);
AdornerLayer.GetAdornerLayer(image1).Add(adorner);
}
当我拖动图像控件时,我的装饰器没有移动。
我尝试过覆盖 ArrangeOverride 和 MeasureOverride:
protected override Size MeasureOverride(Size constraint)
{
Debug.WriteLine("measureoverride");
InvalidateVisual();
return base.MeasureOverride(constraint);
}
protected override Size ArrangeOverride(Size finalSize)
{
Debug.WriteLine("arrangeoverride");
InvalidateVisual();
return base.ArrangeOverride(finalSize);
}
但没有效果。输出窗口中没有任何内容,装饰器也没有移动。
当我缩放时 - 一切都很好。装饰器会根据图像控件的变化来更改其位置。
问题出在我的代码中还是在 ZoomControl 中?
示例应用位于此处。
解决方案:
我必须将画布放入AdornerDecorator中:
<Controls:ZoomControl x:Name="zoomControl1" Background="AliceBlue">
<AdornerDecorator>
<Canvas Width="{Binding Path=SModel.Width}" Height="{Binding Path=SModel.Height}">
<Image HorizontalAlignment="Center"
Name="image1" Stretch="Fill"
VerticalAlignment="Center" Source="/Z;component/Images/00000006.jpg"
Margin="0,0,0,0" Canvas.Left="1"
Width="{Binding Path=SModel.Width}" Height="{Binding Path=SModel.Height}">
<Image.LayoutTransform>
<RotateTransform Angle="{Binding Path=SModel.Angle}" />
</Image.LayoutTransform>
</Image>
</Canvas>
</AdornerDecorator>
</Controls:ZoomControl>
描述为此处。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我查看了您的示例应用程序并使用 snoop 检查了它。因此,似乎转换也以某种方式应用于您的装饰器,但视觉效果未正确更新。事实上,平移是有效的,只是你的装饰器没有正确失效。
一个简单的想法是为缩放、平移 x、平移 y 提供装饰器依赖属性,并设置属性标志“AffectsRender”并将它们从缩放控件绑定到画布。现在,每当您的缩放控件修改这些属性之一时,装饰器属性都会通过绑定进行更新,并通过“AffectsRender”标志直接失效。
[第一个答案,但无效]
您是否将缩放控件的变换应用于您的装饰器?因为装饰器没有放置在控件上,所以实际上它注册在下一个 AdornerDecorator 中,从提供的控件开始向上找到。我的猜测是,找到的唯一装饰器(或层)是窗口上的默认装饰器。将控件的转换应用到您的装饰器上,它应该可以工作。或者您可以在 Zoomcontrol 中放置一个 AdornerDecorator,但说实话,我不确定这是否具有预期的效果。
Ok, i looked into your example app and checked it with snoop. So it seems that the transformation is somehow applied to your adorner aswell, but the visual is not correctly updated. Panning works in fact, its just that your adorner is not properly invalidated.
One quick idea is to give your adorner dependency property for zoom, translate x, translate y, and set the property flag "AffectsRender" and bind them from the zoom control to your canvas. Now everytime your zoom control modifies one of these properties, the adorners properties are updated via binding and directly invalidated via the "AffectsRender" flag.
[First answer, but not valid]
Are you applying the transform of the zoom control to your adorner? Because an adorner is not placed on the control, in fact it is registered in the next AdornerDecorator, found upwards beginning on the provided control. My guess is that the only decorator(or layer) found is the default one on the window. Apply the transform of the control to your adorner and it should work. Or you could place an AdornerDecorator inside your Zoomcontrol, but to be honest i'm not sure if this has the desired effect.