从 ItemsControl.ItemTemplate 内部绑定到 ItemsControl 的模板化父级
从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
绑定对我来说看起来不错,但您没有指定
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?我有类似的要求,以下内容对我有用:
PS my app is in XAML for WinRT
I had similar requirement and the following worked for me:
P.S. my app is in XAML for WinRT
我还没有找到解决这个问题的方法。最后,我不得不使用违反我期望的关注点分离的变通办法,但仍按预期运行。我相信这归结为 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.