Wpf 获取嵌套ItemsControl中的CurrentItem
我有一个itemscontrol,带有一个item模板。此项模板中存在另一个itemscontrol。后一个ItemsControl在其模板中有一个按钮,绑定到此模板的命令需要获取“父”ItemsControl当前项 结构看起来像这样:Wpf 获取嵌套ItemsControl中的CurrentItem,wpf,xaml,Wpf,Xaml,我有一个itemscontrol,带有一个item模板。此项模板中存在另一个itemscontrol。后一个ItemsControl在其模板中有一个按钮,绑定到此模板的命令需要获取“父”ItemsControl当前项 结构看起来像这样: <ItemsControl x:Name="outerItemsControl" ItemsSource={Binding MyCollection}> <ItemsControl.ItemTemplate> &l
<ItemsControl x:Name="outerItemsControl" ItemsSource={Binding MyCollection}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource={Binding MySecondCollection}>
<ItemTemplate>
<DataTemplate>
<Button Command="{Binding MyFantasticCommand}"
CommandParameter="{Binding ????}"/>
</DataTemplate>
</ItemTemplate>
</ItemsControl>
</DataTemplate>
<ItemControl.ItemTemplate>
</ItemsControl>
及
编辑
通常,当我们需要访问items控件中的“当前项”时,我们会执行以下操作:
<ItemsControl x:Name="outerItemsControl" ItemsSource={Binding MyCollection}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding MyCommand}" CommandParameter="{Binding .}"/>
</DataTemplate>
<ItemControl.ItemTemplate>
</ItemsControl>
我希望执行与此示例相同的操作,但是从子项控件访问父项的“当前”项 您似乎希望从内部数据模板使用
MySecondCollection
属性访问对象
这应该起作用:
CommandParameter="{Binding DataContext,
RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}"
“当前项”不是指外部项控件,而是指内部项控件,还是我错了?@Clemens它表示外部项控件“选定项”,即使项控件没有SelectedItem。相当于ItemsTemplate中的{Binding.}。正如您所说,没有SelectedItem。你指的是哪一个?可能是“当前”的,例如带有内部ItemsControl的。是否要使用MySecondCollection属性访问视图模型对象?@Clemens请查看我的编辑。
<ItemsControl x:Name="outerItemsControl" ItemsSource={Binding MyCollection}>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding MyCommand}" CommandParameter="{Binding .}"/>
</DataTemplate>
<ItemControl.ItemTemplate>
</ItemsControl>
CommandParameter="{Binding DataContext,
RelativeSource={RelativeSource AncestorType=ContentPresenter, AncestorLevel=2}}"