83210

How to bind to a source inside a ListBox different from the ItemsSource already specified

Question:

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.

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

The DataContext of the whole page is defined like so:

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

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>

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.

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)

What am I missing ?

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);

Answer1:

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}}}

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}

Recommend

  • How may I explicitly define the datatype of a Google Charts DataTable column after it has been creat
  • ChartKick Show original value rather percentage in PIE and line chart
  • iOS 8.2 [NSUserDefaults standardUserDefaults] returning nil
  • Return list instead of an item Webservice Phone 8.1 UAP
  • Capture reload/endrequest event after server redirect to download file
  • Visualizing 2D Data in a Table
  • Disable Application from sleeping in Windows 8 Metro
  • Jquery Mobile - load script along with page (pagecontainershow)
  • How to use a MultiBinding on DataGridTextColumn?
  • Add items to ComboBox at runtime?
  • Binding ContextMenu Tag to Owner
  • How to use the File System Events API in Swift?
  • Determining if a string has been entered with a leading single quote
  • Binding from within a ResourceDictionary in a Catel WPF UserControl
  • Floating-point number in Excel
  • Different Datacontext for Command and CommandParameter
  • 2-table interaction: insert, get result, insert
  • Microsoft Excel Pivot miscalculation in Sum for positive and negative numbers
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • Drawing a Path in XAML (with binding) vs. C#. One works, one doesn't
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Can't remove headers after they are sent
  • Suppressing passwd when calling sqlplus from shell script
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • Force show.bind execution
  • How can I sort a a table with VBA with given text condition?
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • one Local Olampyad Questions on Informatic in 2011
  • Is it possible to access block's scope in method?
  • Get one-time binding to work for ng-if
  • Why is the size of this struct 32?
  • javascript inside java/jsp code
  • htaccess rewriting URLs with multiple forward slashes
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • QuartzCore.framework for Mono Develop
  • A cron job substitute?
  • KeystoneJS: Relationships in Admin UI not updating
  • Does armcc optimizes non-volatile variables with -O0?
  • Net Present Value in Excel for Grouped Recurring CF