cji*_*ibo 5 wpf mvvm inkcanvas
我好像遇到了路障.我们正在使用带有Prism的MVVM,并且拥有一个需要Ink Canvas的View.我创建了一个从我的ViewModel绑定到View的StrokeCollection.我可以从我的viewmodel设置集合,但是当用户绘制时,更改不会出现在ViewModel中.有没有办法让这项工作?
我的ViewModel中的My Property如下:
private StrokeCollection _strokes;
public StrokeCollection Signature
{
get
{
return _strokes;
}
set
{
_strokes = value;
OnPropertyChanged("Signature");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的XAML绑定线:
<InkCanvas x:Name="MyCanvas" Strokes="{Binding Signature, Mode=TwoWay}" />
Run Code Online (Sandbox Code Playgroud)
出于某种原因,显然InkCanvas从未通知ViewModel任何变化.
Ken*_*art 11
你的方法的问题是你假设InkCanvas创建了StrokeCollection.它没有 - 它只是添加和删除它的项目.如果集合不可用(即是null),绑定将失败,并且InkCanvas不会对它做任何事情.所以:
StrokeCollection示例代码:
public class ViewModel : INotifyPropertyChanged
{
private readonly StrokeCollection _strokes;
public ViewModel()
{
_strokes = new StrokeCollection();
(_strokes as INotifyCollectionChanged).CollectionChanged += delegate
{
//the strokes have changed
};
}
public event PropertyChangedEventHandler PropertyChanged;
public StrokeCollection Signature
{
get
{
return _strokes;
}
}
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Run Code Online (Sandbox Code Playgroud)
和XAML:
<InkCanvas Strokes="{Binding Signature}"/>
Run Code Online (Sandbox Code Playgroud)