views:

150

answers:

1

so I have the following definition for a DataGrid control:

<Custom:DataGrid x:Name="dataGrid" ItemsSource="{Binding Units, Mode=Default}" AutoGenerateColumns="False">

and later in the same XAML document I have the following Content control:

<ContentControl Content="{Binding SelectedUnitResults, Mode=TwoWay}"/>

What I would like to do is have the content control bind to the DataGrid's SelectedItem[0].SelectedUnitResults field on the view model (SelectedItem[0] being a ViewModel for a DataTemplate and SelectedUnitResults being a field that is of a type with a DataTemplate binding as well)

Any insight would be appreciated.

+1  A: 

Hi Firoso,

If I got you properly then the following Binding will work:

<Custom:DataGrid x:Name="dataGrid" 
  ItemsSource="{Binding Units, Mode=Default}" 
  IsSynchronizedWithCurrentItem="True"
  AutoGenerateColumns="False">
<ContentControl Content="{Binding Units/SelectedUnitResults}"/>

Assuming that both ContentControl and DataGrid have common DataContext. Magic happens with with that slash symbol in the Binding. It directs binding mechanism to get CurrentItem from the Units collection, and get from that item value of the SelectedUnitResults property.

But hold on, who has CurrentItem? You see, every time you bind to a collection, WPF creates mediator class of the CollectionView type. This mediator wraps original collection and target is bound to this mediator. WPF doesn't ask whether you want this or not.

Okay, but who updates current item? Selector (first child of ItemsControl) does. You noticed that IsSynchronizedWithCurrentItem property on the DataGrid, right? That's it.

To read more about binding to collections refer to MSDN article: Data Binding Overview - Binding to Collections.

Hope this helps.

Anvaka
that helps ALOT thank you!
Firoso