来自父项控件的 WPF 对象绑定
WPF object binding from parent itemscontrol
我的视图模型中有群组集合 属性,每个群组都有自己的团队集合。
我想通过单击按钮从组中删除一个团队。如何将团队所在的组绑定到命令参数?此解决方案无效:
<ItemsControl ItemsSource="{Binding Groups}" Name="gSource" Tag="{Binding .}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Teams}" Name="tSource">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.RemoveTeamFromGroupCommand, ElementName=gSource}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource ObjectsConverter}">
<Binding Path="."/>
<Binding Path="Tag.Value" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
它正确地绑定了团队,但它绑定的不是组,而是 MS.Internal.NamedObject 未设置的值。
如果您希望组成为您的 MultiBinding 的第二个绑定,则 Tag
是不必要的。相反,只需将它绑定到相对祖先 ItemsControl
的 DataContext
。我对此进行了测试并且有效。
<MultiBinding Converter="{StaticResource ObjectsConverter}">
<Binding Path="."/>
<Binding Path="DataContext" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
</MultiBinding>
@Tam Bui 关于使用父 ItemsControl 的 DataContext 是完全正确的。
但除了“RelativeSource AncestorType”之外,您还可以命名 DataTemplate 中的 UI 元素(您分配了 tSource)并使用它们。
您使用了类似的绑定来绑定命令。
此外,默认情况下绑定为当前数据上下文提供 link。
没有必要给它 Path.
替代解决方案:
<Button Command="{Binding Path=DataContext.RemoveTeamFromGroupCommand, ElementName=gSource}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource ObjectsConverter}">
<Binding/>
<Binding Path="DataContext" ElementName="tSource"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
我的视图模型中有群组集合 属性,每个群组都有自己的团队集合。 我想通过单击按钮从组中删除一个团队。如何将团队所在的组绑定到命令参数?此解决方案无效:
<ItemsControl ItemsSource="{Binding Groups}" Name="gSource" Tag="{Binding .}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Teams}" Name="tSource">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Path=DataContext.RemoveTeamFromGroupCommand, ElementName=gSource}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource ObjectsConverter}">
<Binding Path="."/>
<Binding Path="Tag.Value" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
它正确地绑定了团队,但它绑定的不是组,而是 MS.Internal.NamedObject 未设置的值。
如果您希望组成为您的 MultiBinding 的第二个绑定,则 Tag
是不必要的。相反,只需将它绑定到相对祖先 ItemsControl
的 DataContext
。我对此进行了测试并且有效。
<MultiBinding Converter="{StaticResource ObjectsConverter}">
<Binding Path="."/>
<Binding Path="DataContext" RelativeSource="{RelativeSource AncestorType=ItemsControl}"/>
</MultiBinding>
@Tam Bui 关于使用父 ItemsControl 的 DataContext 是完全正确的。
但除了“RelativeSource AncestorType”之外,您还可以命名 DataTemplate 中的 UI 元素(您分配了 tSource)并使用它们。
您使用了类似的绑定来绑定命令。
此外,默认情况下绑定为当前数据上下文提供 link。
没有必要给它 Path.
替代解决方案:
<Button Command="{Binding Path=DataContext.RemoveTeamFromGroupCommand, ElementName=gSource}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource ObjectsConverter}">
<Binding/>
<Binding Path="DataContext" ElementName="tSource"/>
</MultiBinding>
</Button.CommandParameter>
</Button>