WPF - 路径几何...有没有办法绑定数据属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14167763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF - Path Geometry...Is there a way to bind the Data Property?
提问by tronious
I have a ControlTemplatethat serves as a "Bubble" popup on the AdornerLayerof a given control.
我有一个ControlTemplate用作AdornerLayer给定控件上的“气泡”弹出窗口。
It works fine but I need to be able to calculate where it should display (middle/ bottom).
它工作正常,但我需要能够计算它应该显示的位置(中间/底部)。
Instead of:
代替:
<Path Stroke="Black" Fill="Black" Data="M 15 20 L 15 0 33 20" Margin="0 1 0 0"/>
I am looking for (obviously this won't work but it illustrates what I'm trying to accomplish:
我正在寻找(显然这行不通,但它说明了我正在努力实现的目标:
<Path Stroke="Black" Fill="Black" Data="M {TemplateBinding Left} 20 L 15 0 33 20"/>
Can this be done with a ValueConverter? I just can't visualize a solution for some reason. I'm also open to alternatives.
这可以用 a 来完成ValueConverter吗?由于某种原因,我无法想象解决方案。我也对替代品持开放态度。
Thanks for reading and if I can provide more info please just ask.
感谢阅读,如果我能提供更多信息,请询问。
回答by ColinE
If you want a value converter that you can use to convert a string into the path data, you might like to try the universal value converterI wrote a while back.
如果您想要一个可用于将字符串转换为路径数据的值转换器,您可能想尝试我不久前编写的通用值转换器。
Alternatively, to bind to a single property, you will have to expand your geometry by adding the various geometry objects into your XAML, rather than using the string shorthand. For example ...
或者,要绑定到单个属性,您必须通过将各种几何对象添加到 XAML 中来扩展几何,而不是使用字符串速记。例如 ...
<Path Stroke="Black" StrokeThickness="1">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure IsClosed="True" StartPoint="10,100">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="{Binding MyPropertyPath}" />
<LineSegment Point="100,50" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>

