从 ItemsControl.ItemTemplate 内部绑定到 ItemsControl 的模板化父级

发布于 2024-12-05 19:04:50 字数 877 浏览 1 评论 0原文

从 ItemsControl.ItemTemplate 中绑定到 ItemsControl 的父级的正确方法是什么?

非工作尝试:

<ControlTemplate TargetType="{x:Type local:ParentUserControl}">
        <ItemsControl ItemsSource="{Binding MyCollectionViewSource.View, RelativeSource={RelativeSource TemplatedParent}}"
                  IsTabStop="False"
                  Focusable="False">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:ChildUserControl BoundProp1="{Binding Prop1}"
                     BoundObjProp2="{Binding RelativeSource={RelativeSource    
                     Mode=FindAncestor, 
                     AncestorType={x:Type local:ParentUserControl}}}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>    
        </ItemsControl>
</ControlTemplate>

What is the proper way to bind to the Parent of an ItemsControl from within the ItemsControl.ItemTemplate?

Non working attempt:

<ControlTemplate TargetType="{x:Type local:ParentUserControl}">
        <ItemsControl ItemsSource="{Binding MyCollectionViewSource.View, RelativeSource={RelativeSource TemplatedParent}}"
                  IsTabStop="False"
                  Focusable="False">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <local:ChildUserControl BoundProp1="{Binding Prop1}"
                     BoundObjProp2="{Binding RelativeSource={RelativeSource    
                     Mode=FindAncestor, 
                     AncestorType={x:Type local:ParentUserControl}}}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>    
        </ItemsControl>
</ControlTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朱染 2024-12-12 19:04:50

绑定对我来说看起来不错,但您没有指定 Binding.Path,您确定要直接绑定到控件而不是属性吗?

The binding looks fine to me, but you do not specify a Binding.Path, are you sure that you want to bind directly to the control and not a property?

旧故 2024-12-12 19:04:50

我有类似的要求,以下内容对我有用:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapGrid Orientation="Horizontal" ItemWidth="{Binding ItemWidth}" ItemHeight="{Binding ItemHeight}"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate >
            <views:MyUserControl
                Width="{Binding DataContext.ItemWidth, ElementName=PageRoot}"
                Height="{Binding DataContext.ItemHeight, ElementName=PageRoot}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

PS my app is in XAML for WinRT

I had similar requirement and the following worked for me:

<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapGrid Orientation="Horizontal" ItemWidth="{Binding ItemWidth}" ItemHeight="{Binding ItemHeight}"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate >
            <views:MyUserControl
                Width="{Binding DataContext.ItemWidth, ElementName=PageRoot}"
                Height="{Binding DataContext.ItemHeight, ElementName=PageRoot}"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

P.S. my app is in XAML for WinRT

绅士风度i 2024-12-12 19:04:50

我还没有找到解决这个问题的方法。最后,我不得不使用违反我期望的关注点分离的变通办法,但仍按预期运行。我相信这归结为 wpf 框架中的一个问题,希望 4.5 能够解决它。

I have not found a solution to this problem. In the end I had to use work arounds which violated my desired separation of concerns, but functioned as expected. I believe this comes down to an issue in the wpf framework, hopefully 4.5 will fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文