【问题标题】:WPF object binding from parent itemscontrol来自父项控件的 WPF 对象绑定
【发布时间】:2021-10-01 10:12:51
【问题描述】:

我的视图模型中有 Groups 集合属性,每个组都有其 Teams 集合。 我想通过单击按钮从组中删除一个团队。如何将团队所在的组绑定到命令参数?此解决方案不起作用:

<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 绑定到未设置的值。

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    @Tam Bui 关于使用父 ItemsControl 的 DataContext 是完全正确的。
    但是除了“RelativeSource AncestorType”之外,您还可以在 DataTemplate 中命名 UI 元素(您分配了 tSource)并使用它们。 您使用了类似的绑定来绑定命令。
    此外,默认情况下,绑定提供了指向当前数据上下文的链接。
    没有必要给它路径。
    替代解决方案:

        <Button Command="{Binding Path=DataContext.RemoveTeamFromGroupCommand, ElementName=gSource}">
            <Button.CommandParameter>
                <MultiBinding Converter="{StaticResource ObjectsConverter}">
                    <Binding/>
                    <Binding Path="DataContext" ElementName="tSource"/>
                </MultiBinding>
            </Button.CommandParameter>
        </Button>
    

    【讨论】:

      【解决方案2】:

      如果您希望 Group 成为 MultiBinding 的第二个绑定,则不需要 Tag。相反,只需将其绑定到相对祖先ItemsControlDataContext。我对此进行了测试,它确实有效。

      <MultiBinding Converter="{StaticResource ObjectsConverter}">
          <Binding Path="."/>
          <Binding Path="DataContext" RelativeSource="{RelativeSource  AncestorType=ItemsControl}"/>
      </MultiBinding>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-22
        • 1970-01-01
        相关资源
        最近更新 更多