MVVM 绑定到 InkCanvas
我似乎遇到了障碍。 我们将 MVVM 与 Prism 结合使用,并且有一个需要 Ink Canvas 的视图。 我创建了一个从 ViewModel 绑定到 View 的 StrokeCollection。 我可以从视图模型设置集合,但在用户绘制时视图模型不会发生更改。 有办法让这项工作发挥作用吗?
我的 ViewModel 中的属性如下:
private StrokeCollection _strokes;
public StrokeCollection Signature
{
get
{
return _strokes;
}
set
{
_strokes = value;
OnPropertyChanged("Signature");
}
}
这是我的 XAML 绑定行:
<InkCanvas x:Name="MyCanvas" Strokes="{Binding Signature, Mode=TwoWay}" />
由于某种原因,显然 InkCanvas 永远不会通知 ViewModel 任何更改。
I seem to have ran into a road block. We are using MVVM with Prism and have a View that requires an Ink Canvas. I've create a StrokeCollection that is being bound from my ViewModel to the View. I am able to set the collection from my viewmodel but changes are not coming up to the ViewModel while the user draws. Is there a way to make this work?
My Property in my ViewModel is as follows:
private StrokeCollection _strokes;
public StrokeCollection Signature
{
get
{
return _strokes;
}
set
{
_strokes = value;
OnPropertyChanged("Signature");
}
}
Here is my XAML binding line:
<InkCanvas x:Name="MyCanvas" Strokes="{Binding Signature, Mode=TwoWay}" />
For some reason apparently the InkCanvas never notifies the ViewModel of any change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的方法的问题在于您假设
InkCanvas
创建StrokeCollection
。 它没有 - 它只是添加和删除其中的项目。 如果该集合不可用(即null
),则绑定将失败,并且InkCanvas
不会对其执行任何操作 。 因此:StrokeCollection
示例代码:
和 XAML:
The problem with your approach is that you assume the
InkCanvas
creates theStrokeCollection
. It does not - it merely adds and removes items from it. And if the collection isn't available (ie. isnull
), the binding will fail and theInkCanvas
won't do anything with it. So:StrokeCollection
Example code:
And XAML:
StrokeCollection 类有一个名为“StrokesChanged”的事件,当您在视图中绘制某些内容时,该事件总是会被触发。 该事件包含更新的笔画集合。
XAML:
VM:
希望有帮助!
StrokeCollection class have an event called "StrokesChanged" that is always fired when you draw something in the View. That event contains the collection of strokes updated.
XAML:
VM:
Hope it helps!