MVVM 嵌套数据绑定
当使用具有 ItemsSource 的 ItemsControl 时,我在 UserControl 内部的数据绑定方面遇到一些问题。我的事件触发器从未被调用。
我认为问题是,当我在行中调用 eventtrigger 时:
<cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" />
它尝试在 CheckBoxes 集合中查找选中的事件,因为我已经设置了 ItemsSource,而它应该在其parent 中查找。我几天来一直在寻找解决方案,但似乎都不起作用。
我的代码如下所示:
<Grid x:Name="layoutroot">
<ItemsControl x:Name="itemcontrol" ItemsSource="{Binding CheckBoxes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<s:SurfaceCheckBox Background="White" Foreground="White">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</s:SurfaceCheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
当我尝试以下代码时,它的工作原理与预期完全一致:
<Grid x:Name="layoutroot">
<s:SurfaceCheckBox Background="White" Foreground="White" Content="{Binding Content}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</s:SurfaceCheckBox>
</Grid>
但我确实需要在具有设置的 ItemsSource 的 itemsControl 内执行此行为。
有什么想法吗?
I'm having some trouble with databinding inside a UserControl when using an ItemsControl which has an ItemsSource. My Eventtrigger is never called.
I Think the problem is that when I call my eventtrigger in the line:
<cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" />
it tries to find the checked event in CheckBoxes collection because i have set my ItemsSource, while it should be looking in its parent . I've been searching for a solution for days, but none of them seem to work.
My code looks like this:
<Grid x:Name="layoutroot">
<ItemsControl x:Name="itemcontrol" ItemsSource="{Binding CheckBoxes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<s:SurfaceCheckBox Background="White" Foreground="White">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</s:SurfaceCheckBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
When I try the following code it works exactly as expected:
<Grid x:Name="layoutroot">
<s:SurfaceCheckBox Background="White" Foreground="White" Content="{Binding Content}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<cmd:EventToCommand Command="{Binding ElementName=layoutroot, Path=DataContext.Checked}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</s:SurfaceCheckBox>
</Grid>
But I really need this behaviour inside an itemsControl with a set ItemsSource.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ItemsControl
内部的绑定放置在集合中的当前 Item 上。您需要做的是找到父级,然后从那里绑定。从您的
ItemsControl
内部尝试一下,替换MyUserControlName
:Binding inside of an
ItemsControl
is placed upon the current Item in the collection. What you need to do is seek out the parent, and Bind from there.Give this a try from inside your
ItemsControl
, replacingMyUserControlName
: