2013-03-05 69 views
0

我有一个是具有由通过母体用户控件(Counterparties_MainWindow)到嵌入式用户控件(Counterparties_UserInputs和Counterparties_SystemDetails)的复合视图:绑定到父控制的CollectionViewSource

<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> 

的“Counterparties_SystemDetails”包含一个显示CounterpartiesData(对象列表)中某些已定义字段的网格。 “Counterparties_UserInputs”显示应该由用户定义的CounterpartiesData中尚未定义的字段的列表。

我曾经有过CollectionViewSource在Counterparties_SystemDetails用户控件XAML和直接网格绑定到它,这是工作(即显示我的CounterpartiesData细节):但是

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

,现在我已将CollectionViewSource移动到父窗口为了在两个子控件中共享相同的CounterpartiesData对象,我找不到任何方式将它绑定回我的网格。请您提供一些关于如何操作的提示?

最后但并非最不重要的一点,我想在用户控件Counterparties_UserInputs的文本框中显示当前选定的counterpaty名称。你知道我可以轻松访问吗?

谢谢!

回答

0

已经使用其DataContext你应该能够只通过counterpartiesDataViewDataContextUserControl

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

,并设置的ItemSource你的用户控件的

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

然而IDF您UserControl别的东西,你可以使用FindAncestor找到父母并绑定到它的一个属性。

<DataGrid Name="CounterpartiesGrid" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}},Path=CounterpartiesData}"> 
+0

非常感谢你,这工作非常好!然而,我忘了问问题的最后我已经添加了什么..我需要也显示网格当前选定的对手名称到一个文本框,这是在Counterparties_UserInputs这次。我已经尝试绑定它,但没有找到正确的合成器:'' – goul 2013-03-05 07:28:38

+0

您应该能够绑定'DataGrid',像'Text =“{Binding ElementName = dataGridName,Path = SelectedItem.cptyName}”' – 2013-03-05 07:36:23

+0

对不起,没有正确解释。 datagrid位于“Counterparties_SystemDetails”中,即“Counterparties_UserInputs”中的文本框,它们都指向同一个'DataContext'现在不知道如何从另一个子UserControl访问数据网格。这里是我的错误日志,希望它有帮助:'BindingExpression由于缺少信息而无法检索值。 BindingExpression:路径= SelectedCounterparty;的DataItem = '的ListCollectionView';目标元素是'DataGrid'(Name ='CounterpartiesGrid');目标属性是'SelectedItem'(类型为'Object')' – goul 2013-03-05 07:46:25