0

I have a composite view that is made up by a parent user control (Counterparties_MainWindow) with to embeded user controls (Counterparties_UserInputs and Counterparties_SystemDetails):

<UserControl x:Class="Counterparties_MainWindow">
<av:UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Counterparties_Dictionary.xaml" />
        </ResourceDictionary.MergedDictionaries>
        <CollectionViewSource x:Key="counterpartiesDataView" Source="{Binding Path=CounterpartiesData}" />
    </ResourceDictionary>
</av:UserControl.Resources>

<DockPanel>
   <DockPanel>
       <GUI:Counterparties_UserInputs x:Name="UserInputs"/>
       <GUI:Counterparties_SystemDetails x:Name="SystDetails"/>
   </DockPanel>
</DockPanel>

The "Counterparties_SystemDetails" contains a grid that displays some already defined fields from the CounterpartiesData (a list of objects). The "Counterparties_UserInputs" displays the list of not yet defined fields from the CounterpartiesData that should be defined by the user.

I used to have the CollectionViewSource in the Counterparties_SystemDetails user control xaml and was binding directly the grid to it, it was working (i.e. displaying me the CounterpartiesData details):

<DataGrid Name="CounterpartiesGrid" ItemsSource="{Binding Source={StaticResource counterpartiesDataView}}"
SelectedItem="{Binding Path=SelectedCounterparty, Mode=OneWayToSource}"  Style="{StaticResource DataGridStyle}">

However, now that I've moved the CollectionViewSource to the parent window in order to share the same CounterpartiesData object in both sub controls, I can't find any way to bind it back to my grid. Would you please kindly have some tips on how to do so?

Last but not least, I would like to display the current selected counterpaty name in a text box field of the user control Counterparties_UserInputs. Would you know how I could access it easily?

Thank you!

goul
  • 813
  • 1
  • 13
  • 32

1 Answers1

0

You should be able to just pass the counterpartiesDataView as the DataContext to your UserControl

<DockPanel>
   <DockPanel>
       <GUI:Counterparties_UserInputs x:Name="UserInputs" DataContext="{Binding Source={StaticResource counterpartiesDataView}}" />

And set the ItemSource of your UserControl

<DataGrid Name="CounterpartiesGrid" ItemsSource="{Binding}" />

However idf you UserControl is already using its DataContext for something else you can use FindAncestor to find the Parent and bind to one of its properties.

<DataGrid Name="CounterpartiesGrid" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=CounterpartiesData}">
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • Thank you very much, this works perfectly fine! However I forgot to ask something that I've added at the end of the question.. I would need to also display the current selected counterparty name in the grid into a textbox that is in the Counterparties_UserInputs this time. I've tried binding it but don't find the correct synthax: `` – goul Mar 05 '13 at 07:28
  • you should be able to bind off the `DataGrid`, something like `Text="{Binding ElementName=dataGridName, Path=SelectedItem.cptyName}"` – sa_ddam213 Mar 05 '13 at 07:36
  • Sorry, didn't explained correctly. The datagrid is in the "Counterparties_SystemDetails", the textbox in the "Counterparties_UserInputs", both pointing to that same `DataContext` now so not sure how to access the datagrid from the other sub `UserControl`. Here is my error log, hope it helps: `BindingExpression cannot retrieve value due to missing information. BindingExpression:Path=SelectedCounterparty; DataItem='ListCollectionView'; target element is 'DataGrid' (Name='CounterpartiesGrid'); target property is 'SelectedItem' (type 'Object')` – goul Mar 05 '13 at 07:46
  • Ohhhh.. If you have set the `DataContext` of `UserInputs` to `counterpartiesDataView` you can just access anything inside `counterpartiesDataView` directly, `Text="{Binding cptyName}"` (assuming `cptyName` is a property inside `counterpartiesDataView`) – sa_ddam213 Mar 05 '13 at 07:58
  • No problems, always happy to help :) - Happy coding. – sa_ddam213 Mar 05 '13 at 08:14