Question

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!

Was it helpful?

Solution

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}">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top