问题
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.
In my xaml code (XAML) i have this as a declaration of the 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.
I want to have the C# code that do this, please no XAML code because it is all dynamic !!
回答1:
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 Virtualization
if you create rows manually yourself.
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:
It will show 2 columns and 100 rows filled with your custom user control:
<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 AutoGenerateColumns
to False
and manually add Columns collection. Instead of creating DataGridTemplateColumns
manually you can declare it under resources section of DataGrid and use it in code behind.
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();
}
}
来源:https://stackoverflow.com/questions/23968234/wpf-datagrid-of-usercontrols