将LineGeometry的粗细绑定到值

时间:2014-03-20 07:00:21

标签: c# wpf binding

我已将此代码放在一起,从(0,0)到(100,100)绘制Line。我知道如何Bind StrokeThickness LineGeometry`的Line' to some other value, but here the line is drawn using属性没有该属性。如何将其绑定到另一个值(例如Slider.Value)?

var line = new LineGeometry
{
    StartPoint = new Point(0, 0),
    EndPoint = new Point(100, 100)
};

var geometryGroup = new GeometryGroup();
geometryGroup.Children.Add(line);

var drawing = new GeometryDrawing {Geometry = geometryGroup};
var drawingGroup = new DrawingGroup();
drawingGroup.Children.Add(drawing);

var myPen = new Pen {Thickness = 1, Brush = Brushes.Yellow};
drawing.Pen = myPen;

var myImage = new Image {Stretch = Stretch.None, Margin = new Thickness(10)};

var myDrawingImage = new DrawingImage {Drawing = drawingGroup};
myImage.Source = myDrawingImage;

canvas.Children.Add(myImage);

1 个答案:

答案 0 :(得分:1)

使用您分配给LineGeometry的{​​{1}}对象绘制Pen。因此,只需绑定GeometryDrawing.Pen属性Thickness,就像之前绑定到Pen的{​​{1}}一样。

不同之处在于Thickness没有Line方法,正如Pen所做的那样(继承自SetBinding)。绑定任何Line属性的一般方法是使用FrameworkElement方法:

DependencyObject