我正在尝试绑定WPF path对象上的数据字段。我尝试了两种不同的方法,但都不会触发更新的绘图。
计划1:
<Path Data="{Binding SegmentPoints, Converter={StaticResource PointsToBezier}}" ... />SegmentPoints是一个ObservableCollection<Point>。转换器返回一个新的StreamGeometry对象。我原以为如果我替换了集合中的一个点,那么这个东西就会被重绘。这不管用。如果我为SegmentPoints触发了一个PropertyChanged事件,它就会重绘。
计划2:
<Path ...><Path.Data><PathGeometry Figures="{Binding Figures}" .../>...然后,在视图模型中,我更新像我的BezierSegment.Point3这样的东西。因为各种线段类型上的Point属性都是DependencyProperties,所以我想我可以更新它们,然后它们将触发和更改树。显然也不是这样的。
所以我的问题是:有什么方法可以手动触发对Path对象的更新吗?(如果是这样的话,我可能会使用NotifyOnTargetUpdated。)是否有某种方法可以配置系统,使线段上的点更改将触发重绘?
发布于 2013-04-30 05:50:51
第一种方法中的绑定仅在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属性,而是直接在XAML或代码中分配一个PathFigureCollection。
<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类定义了一个Figures属性,该属性被分配给PathGeometry的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
});https://stackoverflow.com/questions/16288516
复制相似问题