UserControls 的 wpf DataGrid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23968234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wpf DataGrid of UserControls
提问by AouledIssa
I am trying to have a DataGrid that shows a user controls in each cell of it's rows. highliting that the DataGrid have to be dynamic because columns count is dynamic for each case of use.
我正在尝试使用 DataGrid 在其行的每个单元格中显示用户控件。强调 DataGrid 必须是动态的,因为列数对于每种使用情况都是动态的。
In my xaml code (XAML) i have this as a declaration of the DataGrid :
在我的 xaml 代码(XAML)中,我将其作为 DataGrid 的声明:
<Grid Grid.Column="1" Margin="0,10,0,0">
<DataGrid AutoGenerateColumns="False" x:Name="planningTable" FrozenColumnCount="1"/>
</Grid>
My user controle look like this (the UserControl is already done and it works perfectly):

我的用户控件看起来像这样(用户控件已经完成并且运行良好):

As a result of the DataGrid i want to have this UserControl in each Cell of the DataGrid it means that DataGrid Rows have to show this UserControl in each Cell. i've searched a lot for this trick but seems that DataGrid can't host a UserControl in cells.
作为 DataGrid 的结果,我希望在 DataGrid 的每个单元格中都有这个 UserControl,这意味着 DataGrid Rows 必须在每个单元格中显示这个 UserControl。我已经为这个技巧搜索了很多,但似乎 DataGrid 无法在单元格中托管 UserControl。
I want to have the C# code that do this, please no XAML code because it is all dynamic !!
我想要执行此操作的 C# 代码,请不要使用 XAML 代码,因为它都是动态的!!
回答by Rohit Vats
Like I mentioned in comment, you can do that dynamically with XAML only.
Doing this in code behind, you might end up writing lot of code and loose upon important features of WPF. Most importantly UI Virtualizationif you create rows manually yourself.
就像我在评论中提到的那样,您只能使用 XAML 动态执行此操作。在后面的代码中执行此操作,您最终可能会编写大量代码并且对 WPF 的重要功能有所了解。最重要的是,UI Virtualization如果您自己手动创建行。
In case you don't want any binding support and want to show plain dataGrid with all cells filled with your UserControl, you can do this way:
如果您不需要任何绑定支持并希望显示所有单元格都填充有 UserControl 的普通 dataGrid,您可以这样做:
It will show 2 columns and 100 rows filled with your custom user control:
它将显示 2 列和 100 行填充了您的自定义用户控件:
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="EnumerableRange"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
ObjectType="{x:Type linq:Enumerable}" MethodName="Range">
<ObjectDataProvider.MethodParameters>
<sys:Int32>1</sys:Int32>
<sys:Int32>100</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
<DataGrid AutoGenerateColumns="False" IsReadOnly="True"
CanUserAddRows="False"
CanUserDeleteRows="False"
ItemsSource="{Binding Source={StaticResource EnumerableRange}}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Test1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:SampleUserControl/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Test2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:SampleUserControl/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
UPDATE
更新
In case you want to set columns dynamically, like I mentioned in my comments you have to set AutoGenerateColumnsto Falseand manually add Columns collection. Instead of creating DataGridTemplateColumnsmanually you can declare it under resources section of DataGrid and use it in code behind.
如果你要设置列动态,就像我在我的意见,你必须设置提到AutoGenerateColumns要False和手动添加列集合。DataGridTemplateColumns您可以在 DataGrid 的资源部分下声明它并在后面的代码中使用它,而不是手动创建。
XAML:
XAML:
<Grid>
<Grid.Resources>
<ObjectDataProvider x:Key="EnumerableRange"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:linq="clr-namespace:System.Linq;assembly=System.Core"
ObjectType="{x:Type linq:Enumerable}" MethodName="Range">
<ObjectDataProvider.MethodParameters>
<sys:Int32>1</sys:Int32>
<sys:Int32>100</sys:Int32>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
<DataGrid AutoGenerateColumns="False"
x:Name="dataGrid"
IsReadOnly="True"
CanUserAddRows="False"
CanUserDeleteRows="False"
ItemsSource="{Binding Source={StaticResource EnumerableRange}}">
<DataGrid.Resources>
<DataGridTemplateColumn x:Key="TemplateColumn" x:Shared="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<local:SampleUserControl/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Resources>
</DataGrid>
</Grid>
Code behind
背后的代码
public partial class MainWindow : Window
{
private void CreateDataGridColumns()
{
for (int i = 0; i < 10; i++) // Change number of columns here.
{
DataGridTemplateColumn templateColumn =
(DataGridTemplateColumn)dataGrid.Resources["TemplateColumn"];
templateColumn.Header = String.Format("Test {0}", i + 1);
dataGrid.Columns.Add(templateColumn);
}
}
public MainWindow()
{
InitializeComponent();
CreateDataGridColumns();
}
}

