By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

[WPF] Data Binding Hierarchy Issue

EARNEST
100+
P: 128
Hello everybody,

I am new to WPF, and trying to develop a small application using DataBinding following the MVVM paradigm.

I am trying to provide a binding functionality to the ObservableCollection of Documents (my type). The First Label would display a Documents path (available through Attribute property), while the next children - Pages of the Documents - would display appropriate information: page index and page content (an image).

Here is an issue - how can I create a lookup binding to a parent Label? On Button Click command I would like to pass the documents path, which was available earlier in the first DataTemplate.

Is there a way to tackle this issue?
How would you recommend going around it?

Also, is there a better way to tackle "nested" structures (collections within collections)?

Thanks in advance.

Expand|Select|Wrap|Line Numbers
  1. <ItemsControl ItemsSource="{Binding Documents}">
  2.     <ItemsControl.ItemTemplate>
  3.         <DataTemplate>
  4.             <StackPanel>
  5.                 <Label Content="{Binding Path=Attribute.Path}"/>
  6.                     <ItemsControl ItemsSource="{Binding Pages}">
  7.                         <ItemsControl.ItemTemplate>
  8.                             <DataTemplate>
  9.                                 <StackPanel>
  10.                                     <Label Content="{Binding Index}"/>
  11.                                     <Button Content="{Binding Content}"
  12.                                             Command="{x:Static viewModel:DocViewModel.Tests }" 
  13.                                             CommandParameter="{Binding Path=Attribute.Path}"/> 
  14.                                 </StackPanel>
  15.                             </DataTemplate>
  16.                         </ItemsControl.ItemTemplate>
  17.                     </ItemsControl>
  18.                 </StackPanel>
  19.             </DataTemplate>
  20.         </ItemsControl.ItemTemplate>
  21. </ItemsControl>    
  22.  
Dec 3 '12 #1
Share this Question
Share on Google+
1 Reply


EARNEST
100+
P: 128
I received a tip on this issue.

Expand|Select|Wrap|Line Numbers
  1. CommandParameter="{Binding Path=DataContext.Attribute.Path,RelativeSource={RelativeSource AncestorType=ContentPresenter, Mode=FindAncestor,AncestorLevel=2"/> 
  2.  
Dec 4 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.