进入“父母”的数据绑定对象? [英] Getting at the "parent" of a databound object?
问题描述
问题来自于DataTemplate是数据绑定到第一个列表的事实。我期待DataTemplate对包含这两个列表的对象进行数据绑定。现在,发生这种情况,我不知道在DataContext的父中需要什么样的绑定语法,如果这是可能的话。
有人可以指向正确的方向吗?
谢谢!
如果我正确理解你,你可以将ListBox的DataContext设置为一个类的实例(在我的例子中,我通过代码:list.DataContext = myclass;)来设置ItemSource,列表框到类中的列表(即项目)和组合框的项目源到类中的另一个列表(即值)。这是我的xaml似乎有效:
< ListBox Name =listItemsSource ={Binding Items}>
< ListBox.ItemTemplate>
< DataTemplate>
< StackPanel>
< TextBlock Text ={Binding}/>
< ComboBox ItemsSource =
{Binding RelativeSource = {RelativeSource FindAncestor,
AncestorType = {x:Type ListBox}},
Path = DataContext.Values}
/>
< / StackPanel>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
并遵守我所绑定的类:
public class AClass
{
protected List< string> _Items;
public List< string>项目
{
get
{
if(_Items == null)
{
_Items = new List< string>();
}
return _Items;
}
}
protected List< string> _Values;
public List< string>值
{
get
{
if(_Values == null)
{
_Values = new List< string>();
}
return _Values;
}
}
}
在代码中我创建了一个实例的AClass,添加的项目和值,并将实例设置为列表框的DataContext。
I have an object with two arrays of strings. The object is data bound to a listbox. One list is bound as the list's ItemsSource. The other, which is the thing causing me trouble, needs to be bound to a combobox that's part of a DataTemplate that is set to the list's ItemTemplate. Basically each item on the listbox has the corresponding element of the first list and a combo box that contains the second list. In other words, every item in the list has the same combobox of choices.
The problem comes from the fact that it winds up that the DataTemplate is data bound the first list. I was expecting the DataTemplate to be databound to the object that contains both lists. Now, with that happening, I can't figure out what kind of binding syntax I need to get at the "parent" of the DataContext, if that's even possible.
Can someone point me in the right direction?
Thanks!
If I'm understanding you correctly, you can set the DataContext of your ListBox to an instance of a class (in my example I do it in code by: list.DataContext = myclass;) and you want to set the ItemSource of your listbox to a list in the class (ie Items) and the itemssource of your combobox to another list in the class (ie Values). Here's my xaml that seems to work:
<ListBox Name="list" ItemsSource="{Binding Items}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}"/>
<ComboBox ItemsSource=
"{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ListBox}},
Path=DataContext.Values}"
/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and heres the class that I'm binding to:
public class AClass
{
protected List<string> _Items;
public List<string> Items
{
get
{
if (_Items == null)
{
_Items = new List<string>();
}
return _Items;
}
}
protected List<string> _Values;
public List<string> Values
{
get
{
if (_Values == null)
{
_Values = new List<string>();
}
return _Values;
}
}
}
In code I created an instance of AClass, added Items and Values, and set the instance to the DataContext of the listbox.
这篇关于进入“父母”的数据绑定对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!