简体   繁体   English

wpf用户控件的DataGrid

[英]wpf DataGrid of UserControls

I am trying to have a DataGrid that shows a user controls in each cell of it's rows. 我试图有一个DataGrid在其行的每个单元格中显示一个用户控件。 highliting that the DataGrid have to be dynamic because columns count is dynamic for each case of use. 这说明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): 我的用户控件看起来像这样(UserControl已经完成,并且可以正常运行): 在此处输入图片说明

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. 作为DataGrid的结果,我希望在DataGrid的每个单元格中都有此UserControl,这意味着DataGrid行必须在每个Cell中显示此UserControl。 i've searched a lot for this trick but seems that DataGrid can't host a UserControl in cells. 我已经搜索了很多技巧,但似乎DataGrid无法在单元格中托管UserControl。

I want to have the C# code that do this, please no XAML code because it is all dynamic !! 我想拥有执行此操作的C#代码,请不要使用XAML代码,因为它都是动态的!

Like I mentioned in comment, you can do that dynamically with XAML only. 就像我在评论中提到的那样,您只能使用XAML动态地执行此操作。 Doing this in code behind, you might end up writing lot of code and loose upon important features of WPF . 在后面的代码中执行此操作, 您可能最终会写很多代码,而失去WPF的重要功能 Most importantly UI Virtualization if you create rows manually yourself. 最重要的是,如果您自己手动创建行,则是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: 如果您不需要任何绑定支持,并且想要显示纯DataGrid并用UserControl填充所有单元格,则可以执行以下操作:

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 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. 如果要动态设置列(如我在评论中提到的那样),则必须将AutoGenerateColumns设置为False并手动添加Columns集合。 Instead of creating DataGridTemplateColumns manually you can declare it under resources section of DataGrid and use it in code behind. 无需手动创建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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM