WPF用户控件中的DataTemplate内的ItemsControl - 如何绑定到的ItemsSource的父 [英] WPF UserControl in DataTemplate within ItemsControl - how to bind to parent of ItemsSource

查看:876
本文介绍了WPF用户控件中的DataTemplate内的ItemsControl - 如何绑定到的ItemsSource的父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题行说,这一切是真的!我有一个可以成功绑定,比如一个用户控制,全名对象 - 即它的工作原理确定

The subject line says it all really! I have a user control which can be bound successfully to, say, a Fullname object - i.e. it works ok.

我现在需要显示的这些名单,并再次,该工程确定当控制是内ItemsControl.Template一个DataTemplate。

I now need to show a list of these and, again, this works ok when the control is in a DataTemplate within ItemsControl.Template.

这有其ItemControl绑定的FullnameList财产,通过的ItemsSource对象,但是,控制有一个属性(InEditMode)是不是全名对象的属性,但。这InEditMode物业工作正常时,控制不是在一个列表并绑定到命名,比如说,ParentInEditMode和ParentFullname父母兄弟的属性。

But, the control has a property (InEditMode) that is not a property of the Fullname object but of the object that has the FullnameList property to which the ItemsControl is bound, via ItemsSource. This InEditMode property works fine when the control is not in a list and is bound to parent sibling properties named, say, ParentInEditMode and ParentFullname.

现在的问题是 - 前的pression是必需的父对象的编​​辑模式属性为在得到当控制是一个ItemsControl绑定什么样的风格?

The question is - what style of binding expression is required to 'get at' the edit mode property of the parent object when the control is an ItemsControl?

或者,我应该重新设计的全名对象包含EditMode属性?

Or, should I re-design the Fullname object to contain an EditMode property?

提前感谢!

该项目(即认为这是在收集绑定到ItemsControl的)不具有这样的属性。 code是非常简单的:

The item (i.e. that which is in collection bound to the ItemsControl) does NOT have such a property. Code is very simple:

<ItemsControl ItemsSource="{Binding Path=FullnameList}"> 
...then...
<ItemsControl.ItemTemplate>
  <DataTemplate>
    <jasControls:NameView
      NameValue="{Binding Path=.}" 
      InEditMode= ??????? />

整体父(视图模型的窗口)有自己的属性:

The overall parent (the viewmodel for the window) has properties:


  • FullnameList

  • ParentInEditMode

  • 全名(用于测试NameView单个项目,与此XAML使用任何列表控制范围之外的完美作品:

  • FullnameList
  • ParentInEditMode
  • Fullname (single item for testing NameView which works perfectly with this xaml outside of any list control using:

<jasControls:NameView NameValue="{Binding Path=Fullname}" InEditMode="{Binding Path=ParentInEditMode}"/>


我想编辑模式应用到整个集合 - !?让全名的该标志部分看起来不正确

I would like to apply the edit mode to the entire collection - making that flag part of Fullname does not seem right!?

推荐答案

我已经找到了答案,以我自己的问题,我希望能帮助别人。
工作语法我是这样的:

I have found an answer to my own question, which I hope will help others. The working syntax I have is this:

<StackPanel>
    <ItemsControl ItemsSource="{Binding Path=FullnameList}">
    ...then...
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <jasControls:NameView
          NameValue="{Binding Path=.}" 
          InEditMode= "{Binding DataContext.ParentInEditMode,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}" />

此正确地拾取即FullnameList的兄弟和把它传递给数据模板项的属性。更多靠运气比判断,但我希望这是做一个有效的方法!

This correctly picks up the property that is a sibling of FullnameList and passes it to the data template item. More by luck than judgement, but I hope this is a valid way to do this!

这篇关于WPF用户控件中的DataTemplate内的ItemsControl - 如何绑定到的ItemsSource的父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆