为 Infragistics ContentPane 设置数据模板

Set datatemplate for Infragistics ContentPane

我在我的一个用户控件中使用 Infragistics TabGroupPane。我使用 MVVM 进行绑定。当我在另一个用户控件中使用此用户控件时,我想为 Infragistics ContentPane 设置 DataTemplate。我该怎么做?

下面是我正在使用的代码,但它不起作用:

<UserControl.Resources>        
    <DataTemplate DataType="{x:Type infragistics:ContentPane}">
        <DataGrid 
        ItemsSource="{Binding Content, PresentationTraceSources.TraceLevel=High}" IsReadOnly="True" HeadersVisibility="Column" SelectionUnit="FullRow" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn_1" 
            CanUserReorderColumns="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsReorderSupported}"  
            CanUserSortColumns="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsColSortable}"/>
    </DataTemplate>

    <DataTemplate DataType="{x:Type infragistics:PaneHeaderPresenter}">
        <DataGrid 
        ItemsSource="{Binding Content, PresentationTraceSources.TraceLevel=High}" IsReadOnly="True" HeadersVisibility="Column" SelectionUnit="FullRow" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn_1" 
            CanUserReorderColumns="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsReorderSupported}"  
            CanUserSortColumns="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsColSortable}"/>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <ContentControl Name="someContent" Content="{Binding InfraTabs}" Visibility="Collapsed"/>

</Grid>

注意:InfraTabs 是 ContentPane 的集合。

谢谢,

RDV

我仍在寻找答案,但我找到了解决此问题的方法:

解决方法:我创建了一个虚拟 InfraTabViewModel 并将 ContentPane 的内容分配给该 ViewModel。在我看来,我为 InfraTabViewModel 添加了一个数据模板并且它有效! 下面是解决方法代码: 在视图中:

        <DataTemplate DataType="{x:Type vm:InfraTabViewModel}">
        <DataGrid 
        ItemsSource="{Binding DGView, PresentationTraceSources.TraceLevel=High}" IsReadOnly="True" HeadersVisibility="Column" 
            SelectionUnit="FullRow" AutoGeneratingColumn="DataGrid_AutoGeneratingColumn_1" 
            CanUserReorderColumns="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsReorderSupported}"  
            CanUserSortColumns="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.IsColSortable}"/>
    </DataTemplate>

在视图模型中:

    public class InfraTabViewModel
{
    public object DGView { get; set; }
}

public class MyViewModel
{
    ObservableCollection<ContentPane> InfraTabs = new ObservableCollection<ContentPane>();

    public ObservableCollection<ContentPane> InfraTabs
    {
        get;
        set;
    }

    public MyViewModel()
    {
        DataTable dt=new DataTable();
        //Code to populate the dataTable

        ContentPane cp = new ContentPane();
        cp.Header = "Some DataGrid";
        cp.Content = dt.AsDataView(); // does not work.
        cp.Content = new InfraTabViewModel { DGView = dt.AsDataView() }; // works.
    }
}