WPF DataContext 绑定未发生
我有一个用户控件,它动态嵌入许多基于类似数据模板的其他控件。
我的控件有一个 ViewModel,该 ViewModel 具有托管子控件的 DataContext 和 ContentTemplate 的可绑定属性。但是,当我运行模板时,模板绑定良好并初始化,但 DataContext 永远不会被拾取,即使使用 DataContextChanged 事件也是如此。
<ContentControl Name="SessionControl1" DataContext="{Binding SessionItem1}" ContentTemplate="{Binding Source={StaticResource ViewModel},Path=Session1Template}" />
现在作为测试,我采用了我想要的可切换控件之一(AudioCard.xaml)并尝试直接在同一位置使用它,如下所示:
<local:AudioCard DataContext="{Binding SessionItem1}" />
这工作正常,控件初始化就像在动态情况下一样并且 DataContextChanged 事件触发使用正确的绑定。这让我认为绑定到 ContentControl 中的 DataContext 不起作用。
两个问题: 1)有没有简单的方法可以修复它?
2)有更好的方法吗?
编辑:根据第一个答案,我将代码更改为如下所示:
<ContentControl Name="SessionControl1" Grid.Row="0" Grid.Column="0" ClipToBounds="False" Height="128" Width="128"
DataContext="{Binding SessionItem1}"
ContentTemplateSelector="{StaticResource ProximitySessionCardTemplateSelector}" />
我可以看到代码进入我的内容模板选择器,但选择器中的对象是空的。我应该绑定其他东西吗?
I have a user control that embeds dynamically a number of other controls based on similar DataTemplates.
My control has a ViewModel with bindable properties for both the DataContext of the hosted sub-control and the ContentTemplate. However, when I run the template binds fine and initializes but the DataContext never gets picked up, even with a DataContextChanged event.
<ContentControl Name="SessionControl1" DataContext="{Binding SessionItem1}" ContentTemplate="{Binding Source={StaticResource ViewModel},Path=Session1Template}" />
Now as a test, I took one of the switchable controls that I wanted (AudioCard.xaml) and tried using it in the same place directly like so:
<local:AudioCard DataContext="{Binding SessionItem1}" />
This works fine, the control initializes just like in the dynamic case AND the DataContextChanged event fires with the correct binding. This leads me to think that binding to the DataContext in the ContentControl is not working.
Two questions:
1) is there an easy way to fix it?
2) is there a better way to do it?
EDIT: Based on first answer, I changed the code to look like this:
<ContentControl Name="SessionControl1" Grid.Row="0" Grid.Column="0" ClipToBounds="False" Height="128" Width="128"
DataContext="{Binding SessionItem1}"
ContentTemplateSelector="{StaticResource ProximitySessionCardTemplateSelector}" />
I can see the code going into my content template selector but my object in the selector is empty. Should I be binding to something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,根据 Elad 在 XAML 中使用数据模板选择器的建议,并正确绑定到 Content 而不是 DataContext,此代码可以按预期工作:
Ok, based on Elad's advice of using the data template selector in XAML and with the correct binding to Content instead of DataContext, this code works as desired:
关于问题 2:
为什么要绑定 ControlTemplate?
您可以简单地创建一个“捕获”特定类型(ViewModel 中的属性)的 DataTemplate,因此将根据您自动放入的对象显示一个模板,而无需绑定 ControlTemplate。
如果您在 ViewModel 中持有 ControlTemplate 属性(看起来确实如此),那么您就违反了 MVVM 最重要的规则之一 - ViewModel 对 View 一无所知,并且持有没有任何对控件或 UI 的引用。
As to Q2:
Why are you binding to ControlTemplate?
You can simply create a DataTemplate that "catches" a specific type (the property in the ViewModel) and there fore will show a template based on the object you put inside automatically, without the need to bind ControlTemplate as well.
If you're holding ControlTemplate property in your ViewModel (and it seems like you do), you're violated one of MVVM's most important rules - The ViewModel knows nothing about the View, and holds no references to Controls or UI what-so-ever.