只有當SegmentPoint
屬性發生更改時(即分配給新集合),纔會更新第一種方法中的綁定。因此它不需要是ObservableCollection。只需創建一個新的收藏集並提高PropertyChanged事件。
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private ICollection<Point> segmentPoints;
public ICollection<Point> SegmentPoints
{
get { return segmentPoints; }
set
{
segmentPoints = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("SegmentPoints"));
}
}
}
}
然而,在你的第二個辦法,關鍵是不給的PathGeometry的Figure
屬性綁定,而是直接分配PathFigureCollection,無論是在XAML或代碼。
<Canvas Background="Transparent" MouseMove="Canvas_MouseMove">
<Path Stroke="Blue" StrokeThickness="3">
<Path.Data>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection x:Name="figures"/>
</PathGeometry.Figures>
</PathGeometry>
</Path.Data>
</Path>
</Canvas>
中的代碼添加段和修改其屬性:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var segment = new BezierSegment(new Point(100, 0), new Point(200, 300), new Point(300, 100), true);
var figure = new PathFigure();
figure.Segments.Add(segment);
figures.Add(figure);
}
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
var firstSegment = figures[0].Segments[0] as BezierSegment;
firstSegment.Point2 = e.GetPosition(sender as IInputElement);
}
}
您也可以在代碼中創建Figures
屬性,如下圖所示。 MainWindow類定義分配給PathGeometry的Figures
屬性的Figures
屬性。
<Canvas Background="Transparent" MouseMove="Canvas_MouseMove">
<Path Stroke="Blue" StrokeThickness="3">
<Path.Data>
<PathGeometry x:Name="geometry"/>
</Path.Data>
</Path>
</Canvas>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var segment = new BezierSegment(new Point(100, 0), new Point(200, 300), new Point(300, 100), true);
var figure = new PathFigure();
figure.Segments.Add(segment);
Figures = new PathFigureCollection();
Figures.Add(figure);
geometry.Figures = Figures;
}
public PathFigureCollection Figures { get; set; }
private void Canvas_MouseMove(object sender, MouseEventArgs e)
{
var firstSegment = Figures[0].Segments[0] as BezierSegment;
firstSegment.Point2 = e.GetPosition(sender as IInputElement);
}
}
顯然,更新的路徑數據將不會在綁定的Figures
物業工作。以下綁定只分配一次數字,但稍後的更改不會反映在路徑中。
// replace
// geometry.Figures = Figures;
// by
BindingOperations.SetBinding(geometry, PathGeometry.FiguresProperty,
new Binding
{
Path = new PropertyPath("Figures"),
Source = this
});
儘管如此,對於「是否有某種方法可以手動觸發路徑對象的更新?」並不完全是答案。 – Clemens 2013-04-29 22:03:42
我有一個2000段的列表。我不太喜歡重新創建2000個細分的想法,而有人在屏幕上拖動一個點。 – Brannon 2013-04-29 22:03:46
您是否遇到任何性能問題? – Clemens 2013-04-29 22:04:25