简体   繁体   English

如何在WPF中覆盖线的StrokeThickness属性

[英]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. 唯一令我困扰的是LineStrokeThickness也被缩放了。 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.

相关问题 如何在后面的代码中将Line.StrokeThickness属性与Slider值绑定? - How to bind Line.StrokeThickness property with Slider value in code behind? 当我们在网格 WPF 的右端绘制线条时,线条的 StrokeThickness 无法正常工作 - StrokeThickness of Line is not working properly when we draw the line at the right end of the Grid WPF 在Silverlight中以高StrokeThickness绘制线条 - Draw line with high StrokeThickness in silverlight 如何在 WPF C# 中覆盖(使用)BitmapFrame.Thumbnail 属性? - How to override(use) BitmapFrame.Thumbnail property in WPF C#? 如何覆盖 WPF ItemsControl 中预定义依赖属性 ItemsSource 的 PropertyChangedCallback - How to Override PropertyChangedCallback of a predefined Dependency Property ItemsSource in a WPF ItemsControl 绘制WPF形状,作为一个矩形:如果Stroke透明,则StrokeThickness减半 - Drawing a WPF shape, as a Rectangle: StrokeThickness halve if Stroke is Transparent WPF覆盖依赖项属性metdata无法正常工作 - WPF override dependency property metdata is not working WPF在XAML中的继承类中使用override属性 - WPF use override Property in inherit class in XAML 如何重写userControl属性 - How to override userControl property 如何覆盖抽象属性? - How to Override abstract Property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM