【问题标题】:Change element location WPF更改元素位置 WPF
【发布时间】:2013-06-10 09:54:40
【问题描述】:
如何在 C# 代码中更改位置元素(行)?
<Grid x:Name="SetShipsGrid">
<Path Name="Line" Stroke="red" StrokeThickness="1" >
<Path.Data >
<GeometryGroup>
<LineGeometry StartPoint="50,50" EndPoint="350,50"></LineGeometry>
</GeometryGroup>
</Path.Data>
</Path>
</Grid>
【问题讨论】:
标签:
wpf
path
pathgeometry
【解决方案1】:
您可以将起点和终点绑定到公共属性
Xaml:
<Grid x:Name="SetShipsGrid">
<Path Name="Line" Stroke="red" StrokeThickness="1" >
<Path.Data >
<GeometryGroup>
<LineGeometry StartPoint="{Binding StartPoint}" EndPoint="{Binding EndPoint}" />
</GeometryGroup>
</Path.Data>
</Path>
</Grid>
代码:
private Point _startPoint = new Point(5, 5);
private Point _endPoint = new Point(100, 100);
public Point StartPoint
{
get { return _startPoint; }
set { _startPoint = value; NotifyPropertyChanged(); }
}
public Point EndPoint
{
get { return _endPoint; }
set { _endPoint = value; NotifyPropertyChanged(); }
}
【解决方案2】:
在后面的代码中添加名称到行:
<Grid x:Name="SetShipsGrid">
<Path Name="Line" Stroke="red" StrokeThickness="1" >
<Path.Data >
<GeometryGroup>
<LineGeometry x:Name="line" StartPoint="50,50" EndPoint="350,50"></LineGeometry>
</GeometryGroup>
</Path.Data>
</Path>
</Grid>
然后在xaml.cs中:
this.line.StartPoint = ....