MVVM Light - 主/子视图和依赖属性
在实现主/子视图和自定义依赖属性时,我遇到了一个奇怪的问题。
在我的 masterView 中,我在 XAML 中以声明方式绑定视图模型,如下所示:
DataContext="{Binding MainViewModelProperty, Source={StaticResource Locator}}"
并且我的 MainViewModel 公开了一个可观察集合,我将其绑定到 ItemsControl,如下所示:
<ItemsControl ItemsSource="{Binding Lists}" Height="490" Canvas.Top="10" Width="70">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<local:TaskListControl Canvas.Left="{Binding ListLeft}"
Canvas.Top="{Binding ListTop}"
Width="{Binding ListWidth}"
Height="{Binding ListHeight}"
ListDetails="{Binding}"/>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
TaskListControl 依次声明并绑定到它的 ViewModel,并且我还定义了ListDetails 属性的依赖属性。
ListDetails 属性未设置,如果我删除对其视图模型的声明性引用,则依赖属性的回调会被触发。
以声明方式绑定到视图模型和定义依赖属性是否存在冲突?
我真的很喜欢 MVVM Light 的可混合性,并且希望继续解决这个问题,因此我们将不胜感激。
如果您想收到我的项目的源代码,请询问
I'm getting an odd problem when implementing a master / child view and custom dependency properties.
Within my masterView I'm binding the view model declaratively in the XAML as follows:
DataContext="{Binding MainViewModelProperty, Source={StaticResource Locator}}"
and my MainViewModel is exposing an observable collection which I am binding to an ItemsControl as follows:
<ItemsControl ItemsSource="{Binding Lists}" Height="490" Canvas.Top="10" Width="70">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Canvas>
<local:TaskListControl Canvas.Left="{Binding ListLeft}"
Canvas.Top="{Binding ListTop}"
Width="{Binding ListWidth}"
Height="{Binding ListHeight}"
ListDetails="{Binding}"/>
</Canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
TaskListControl in turn declares and bind to it's ViewModel and I've also defined a dependency property for the ListDetails property.
The ListDetails property is not being set and if I remove the declarative reference to it's viewmodel the dependency property's callback does get fired.
Is there a conflict with declaratively binding to viewmodels and definig dependency properties?
I really like MVVM Light's blendability and want to perserve with this problem so any help would be apprectiated.
If you'd like to receive the source for my project then please ask
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定我完全理解你的问题,但让我们尝试猜测。当你谈论“声明性地绑定到视图模型”时,你实际上是指“命令式”,如“在代码中”而不是“在 XAML 中”吗?
如果是这种情况,那么您需要了解这是重写从父级继承的 DataContext,并且 ListDetails 属性现在引用 TaskListControl DataContext,而不再引用 DataTemplate 的 DataContext。
然而,这很容易改变,例如:
这可行吗?
洛朗
I am not sure I totally understand your problem, but let's try and guess.When you talk about "declaratively binding to viewmodel", do you actually mean "imperatively", as in "in the code" instead of "in the XAML"?
If this is the case, then you need to understand that this is overriding the DataContext inheritance from the parent, and the ListDetails property now refers to the TaskListControl DataContext, and not to the DataTemplate's DataContext anymore.
This is easy to change however, for example with:
Would that work?
Laurent