WPF 从子 ItemsControl 数据模板内部绑定到父 ItemsControl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17186985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:02:03  来源:igfitidea点击:

WPF Binding to parent ItemsControl from inside of child ItemsControl data template

c#.netwpfbinding

提问by Robert Petz

I need to be able to bind to a parent ItemsControl's properties from inside of a child ItemsControl data template:

我需要能够ItemsControl从 child ItemsControl 数据模板内部绑定到 parent的属性:

<ItemsControl ItemsSource="{Binding Path=MyParentCollection, UpdateSourceTrigger=PropertyChanged}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>

                <ItemsControl ItemsSource="{Binding Path=MySubCollection}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=MyParentCollection.Value, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Let's assume that MyParentCollection (the outer collection) is of the following type:

让我们假设 MyParentCollection(外部集合)属于以下类型:

public class MyObject
{
    public String Value { get; set; }
    public List<MyChildObject> MySubCollection { get; set;
}

And let's assume that MyChildObject from the above class is of the following type:

让我们假设来自上述类的 MyChildObject 是以下类型:

public class MyChildObject
{
    public String Name { get; set; }
}

How can I bind to MyParentCollection.Value from inside of the inner data template? I can't really use a FindAncestor by type because they both all levels use the same types. I thought maybe I could put a name on the outer collection and use an ElementName tag in the inner binding, but that still couldn't resolve the property.

如何从内部数据模板内部绑定到 MyParentCollection.Value?我不能真正按类型使用 FindAncestor,因为它们所有级别都使用相同的类型。我想也许我可以在外部集合上放置一个名称并在内部绑定中使用 ElementName 标记,但这仍然无法解析该属性。

Any thoughts?

有什么想法吗?

回答by blindmeis

saving the parent item in the tag of the child itemscontrol could work

将父项保存在子项控件的标签中可以工作

    <DataTemplate>

            <ItemsControl ItemsSource="{Binding Path=MySubCollection}" Tag="{Binding .}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=Tag.Value, RelativeSource={RelativeSource  AncestorType={x:Type ItemsControl}}}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

    </DataTemplate>

its not tested but give you a hint in the right direction :)

它没有经过测试,但会给你一个正确方向的提示:)

回答by ASh

Binding for Tag, as suggested in the other answer, is not required. All data can be obtained from DataContext of ItemControl (and this markup Tag="{Binding}"just copies DataContext into Tag property, which is redundant).

Tag正如另一个答案中所建议的那样,不需要绑定 for 。所有数据都可以从ItemControl的DataContext中获取(而且这个标记Tag="{Binding}"只是将DataContext复制到了Tag属性中,是多余的)。

<ItemsControl ItemsSource="{Binding Path=MyParentCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Path=MySubCollection}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Path=DataContext.Value, RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>