我正在 WPF 画布中创建一条折线,假设在点击按钮时根据我的模型中发生的计算更新它的位置。我正在使用 MVVM 模式。
我的 XAML 代码:
<Grid>
<StackPanel>
<Button Margin="10" Command="{Binding Path=RunAnalysisCmd}">Rune analysis!</Button>
<Canvas>
<Polyline Points="{Binding ModelPathPoints, UpdateSourceTrigger=PropertyChanged}" Stroke="Blue" StrokeThickness="2"/>
</Canvas>
</StackPanel>
</Grid>
在我的 ViewModel 中,我有一个 PointCollection 属性,其中存储了路径点。
private PointCollection _modelPathPoints = new PointCollection();
public PointCollection ModelPathPoints
{
get { return _modelPathPoints; }
set
{
_modelPathPoints = value;
NotifyPropertyChanged("ModelPathPoints");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
RunAnalysis 方法运行良好,我已经使用 Consol 输出对其进行了测试。我的问题是当 PointCollection 中的点发生变化时画布没有变化。
public void RunAnalysis()
{
double angle = 0;
for (int i = 0; i < 1000; i=i+10)
{
Model.TranslateModelEdgeNodes(i, i);
Model.RotateModelEdgeNodes(angle);
angle = angle + 0.1;
AddModelPointsToPointCollection();
System.Threading.Thread.Sleep(500);
}
}
public void AddModelPointsToPointCollection()
{
//ModelPathPoints.Clear();
PointCollection modelPathPoints = new PointCollection();
for (int i = 0; i < Model.ModelEdgeNodes.Count(); i++)
{
modelPathPoints.Add(new Point( XXX, XXX )) // Not important what XXX, XXX is
}
modelPathPoints.Add(new Point(XXX, XXX )); // Not important what XXX, XXX is
ModelPathPoints = modelPathPoints;
}
有没有人看到问题??