[英]How to override the StrokeThickness property of a Line in WPF
Here is the problem that has been with me for the past several days. 这是过去几天来我一直遇到的问题。 I have 10000+ Line
objects on a Canvas
and a Slider
that I use to scale the canvas to create a zoom effect. 我在Canvas
上有一个10000+ Line
对象,并使用一个Slider
来缩放画布以创建缩放效果。 Everything is OK and the performance is great. 一切都很好,性能也很棒。
The only thing that bothers me is that the StrokeThickness
of the Line
gets scaled too. 唯一令我困扰的是Line
的StrokeThickness
也被缩放了。 I tried to Bind
the StrokeThickness
to the inverse of the slider's value so that I get a uniform thickness whenever I move the slider. 我试图将StrokeThickness
Bind
到滑块值的倒数,以便每当移动滑块时都获得均匀的厚度。 This worked but has decreased the performance substantially. 这项工作有效,但是却大大降低了性能。
Is there any other way that would be suitable to this situation. 是否有其他适合这种情况的方法。 I want to have a zero-width line thickness no matter how far I scale the canvas (like in CAD programs). 无论我缩放画布多远(例如在CAD程序中),我都希望线宽为零。
For the effect you want to achieve, overriding the StrokeThickness
doesn't really mean anything as the it's value actually remains the same even if the ScaleTransform
is changed. 对于您想要实现的效果,覆盖StrokeThickness
并没有任何意义,因为即使ScaleTransform
更改了,其值实际上仍保持不变。
The approach that you tried - Bind
the StrokeThickness
to the inverse of the slider's value - seems to be the most plausible one. 您尝试的方法-将StrokeThickness
Bind
到滑块值的倒数-似乎是最合理的方法。 To improve performance, instead of setting the StrokeThickness
directly to inverse of Slider's value, set it to another property on your view model. 为了提高性能,不要将StrokeThickness
直接设置为Slider值的倒数,而是将其设置为视图模型上的另一个属性。
Then, in the event where sliding is completed, set the value of that property to the inverse of StrokeThickness
. 然后,在完成滑动的情况下,将该属性的值设置为StrokeThickness
的倒数。
Example - 范例-
private void OnSlidingComplete(object sender, EventArgs e)
{
LineStrokeThickness = GetInverseOfZoomValue();
}
I know that this is not exactly a solution, rather a workaround. 我知道这不是完全解决方案,而是一种解决方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.