如何绑定到与已指定的ItemsSource不同的ListBox内的源 [英] How to bind to a source inside a ListBox different from the ItemsSource already specified

查看:120
本文介绍了如何绑定到与已指定的ItemsSource不同的ListBox内的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HubSection中有一个ListBox,它的项目绑定到通过后面的代码添加到我的DefaulViewModel中的玩家"类. 首先,我简单地将一个TextBox绑定到类"players"的属性"PlayerName"上. 现在,我想添加一个ComboBox,其中包含一些不属于班级玩家的项目.

I have a ListBox inside a HubSection, whose Items are bound to a class "players" added to my DefaulViewModel via code behind. First I simply put a TextBox bound to the property "PlayerName" of my class "players". Now I would like to add a ComboBox with some items that are NOT part of the class players.

有可能吗?我以为在ComboBox中定义ItemsSource会覆盖ListBox的ItemsSource,但是什么也不会显示.

Is it possible ? I thought that definind an ItemsSource in the ComboBox would sort of override the ItemsSource of the ListBox, but nothing displays.

整个页面的DataContext定义如下:

The DataContext of the whole page is defined like so:

DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"

然后,HubSection就像这样:

Then the HubSection is like so:

<HubSection x:Name="HubSec1">
        <DataTemplate>                    
            <ListBox x:Name="ListBox1" ItemsSource="{Binding players}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                       <StackPanel>
                            <TextBox Text="{Binding Path=PlayerName, Mode=TwoWay}"/>
                            <ComboBox ItemsSource="{Binding Path=ListOfElements}"/>                                                                                                                                                             
                        </StackPanel>
                     </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DataTemplate>
</HubSection>

如果我以相同的方式定义ComboBox,但在ListBox之外,它将正确显示"ListOfElements"的字符串元素. 但是在此列表框中,组合框为空.所以我的猜测是,已经为ListBox定义了ItemsSource,无法覆盖它.

If I define the ComboBox in the same way but outside the ListBox, it will display the string elements of "ListOfElements" properly. But in this ListBox, the ComboBox is empty. So my guess is that having defined an ItemsSource for the ListBox, it is not possible to override it.

我试图定义一个DataTemplate,但是没有成功,但是这可能是一个很好的解决方案(并且我没有正确地进行操作)

I have tried to define a DataTemplate but was not successful doing so, but it might be the good solution (and I did not proceed properly)

我想念什么?

ComboBox项是一个ObservableCollection.它不是玩家"类的一部分. 这是我将这些元素添加到DefaultViewModel中的方法

Edit : The ComboBox items is an ObservableCollection. It is not part of the "players" class. Here is how I added these elements to the DefaultViewModel

 DefaultViewModel.Add("players", players);
 DefaultViewModel.Add("MyItemsList", ListOfElements);

推荐答案

您可以走到可视化树上并绑定到祖先数据上下文:

You can walk up the visual tree and bind to an ancestors datacontext:

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}

EX:

{Binding Path=ListOfItems, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}

应该为您提供列表框所具有的数据上下文,因此假设您的ListOfItems存在于该数据上下文中.

that should give you the datacontext that the listbox has, so assuming your ListOfItems exists in that data context.

或者您可以命名控件,然后按元素名称绑定到其数据上下文:

Or you can name your control, and then bind to its datacontext by element name:

{Binding ElementName=mySourceElement,Path=ListOfItems}

这篇关于如何绑定到与已指定的ItemsSource不同的ListBox内的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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