我有一个用户控件,它将xamdatagrid添加到网格中:
<Grid Name="grid1">
<Grid.Resources>
<Style TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding ElementName=grid1, Path=DataContext.Test}"/>
<Setter Property="DisplayMemberPath" Value="Key"/>
</Style>
</Grid.Resources>
<igDP:XamDataGrid DataSource="{Binding}" Name="xamDataGrid1" VerticalAlignment="Top" >
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:Field Name="Name" >
<igDP:Field.Settings>
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}"/>
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Grid>
我在构造函数的代码隐藏中为grid1设置DataContext。这适用于WPF 4.0但不适用于3.5。使用3.5,我得到&#34;无法找到带参考的绑定源&#39; ElementName = grid1&#39;&#34;错误。为了使绑定与3.5一起工作,我不得不使用ObjectDataProvider:
<Grid Name="grid1">
<Grid.Resources>
<ObjectDataProvider x:Key="myData" ObjectType="{x:Type local:ViewModel}"></ObjectDataProvider>
<Style TargetType="{x:Type igEditors:XamComboEditor}">
<Setter Property="ItemsSource" Value="{Binding Source={StaticResource myData}, Path=Test}"/>
<Setter Property="DisplayMemberPath" Value="Key"/>
</Style>
</Grid.Resources>
<igDP:XamDataGrid DataSource="{Binding}" Name="xamDataGrid1" VerticalAlignment="Top" >
<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:Field Name="Name" >
<igDP:Field.Settings>
<igDP:FieldSettings EditorType="{x:Type igEditors:XamComboEditor}"/>
</igDP:Field.Settings>
</igDP:Field>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</Grid>
为什么第一个代码段中的ElementName绑定不能与WPF 3.5一起使用或者我在那里做错了,有没有任何已知的原因?谢谢你。