1

I have the following data model, which is a collection within a collection:

var details1 = new ObservableCollection<Class2>();
details1.Add(new Class2() { Title = "text1" });
details1.Add(new Class2() { Title = "text2" });

var details2 = new ObservableCollection<Class2>();
details2.Add(new Class2() { Title = "text3" });
details2.Add(new Class2() { Title = "text4" });

var Items = new ObservableCollection<Class1>();
items.Add(new Class1() { IsActive = false, Details = details1 });
items.Add(new Class1() { IsActive = true, Details = details2 });

Next, I have a ListBox with ItemsSource bound to Items. The DataTemplate of this list contains another ListBox and its ItemsSource is bound to Details.

In short, my XAML is displaying a list of items and each item is showing a list of item details.

The problem that I have is that I do not know how to bind the value of IsActive property, which belongs to Class1, inside of DataTemplate of the inner ListBox which is associated with Class2 items.

I played with

"{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=DataContext.IsActive}"

but that doesn't work.

Boris
  • 9,986
  • 34
  • 110
  • 147
  • maybe, change to `AncestorType={x:Type ListBox}`? – ASh Mar 15 '16 at 12:42
  • Possible duplicate of [Data binding outside the visual tree. Data Context bridging](http://stackoverflow.com/questions/5647055/data-binding-outside-the-visual-tree-data-context-bridging) – Abin Mar 15 '16 at 13:09
  • Doesn't mode need declared? I may be wrong but if your DataContext is actually from ListBoxItem then it seems like `...{RelativeSource Mode=FindAncestor, ...` should work. – Chris W. Mar 15 '16 at 15:32
  • @ChrisW. You are correct, the Mode does need to be declared, however, typing `FindAncestor` or `Mode=FindAncestor` does not make a difference, as `Mode` is the default parameter for the `RelativeSource`, and as such WPF understands that FindAncestor represents a value for Mode. – Boris Mar 17 '16 at 12:32
  • @Boris Sorry brother but that's incorrect. The [default](https://msdn.microsoft.com/en-us/library/system.windows.data.binding.relativesource%28v=vs.110%29.aspx) is null. However I see what I think you actually meant in that FindAncestor doesn't require Mode= specified to interpret relative location. If you haven't found a remedy to this yet I'll try to swing back and give it some attention if time permits. Cheers! – Chris W. Mar 17 '16 at 15:34

0 Answers0