我在我的一个用户控件中使用Infragistics TabGroupPane。我使用MVVM进行绑定。当我在另一个usercontrol中使用此usercontrol时,我想为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
答案 0 :(得分:0)
我仍在寻找答案,但我找到了解决此问题的方法:
替代方法:我创建了一个虚拟的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>
在ViewModel中:
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.
}
}