wpf用户控件的DataGrid [英] wpf DataGrid of UserControls

查看:102
本文介绍了wpf用户控件的DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个DataGrid,以在其行的每个单元格中显示一个用户控件。强调了DataGrid必须是动态的,因为每种使用情况下列数都是动态的。

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.

在我的xaml代码(XAML)中,我将此作为DataGrid的声明:

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>

我的用户控件看起来像这样(UserControl已经完成并且可以正常工作):

My user controle look like this (the UserControl is already done and it works perfectly):

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

我想拥有可以执行此操作的C#代码此,请不要使用XAML代码,因为它都是动态的!

I want to have the C# code that do this, please no XAML code because it is all dynamic !!

推荐答案

就像我在评论中提到的那样,您可以使用仅XAML。
在后面的代码中执行此操作,您可能最终会编写大量代码,而松散了WPF的重要功能。最重要的是,如果您自己手动创建行,则 UI虚拟化

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.

如果您不想提供任何绑定支持,并且希望显示纯DataGrid并用UserControl填充所有单元格,则可以这样做:

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:

它将显示2列和100行,其中填充了您的自定义用户控件:

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>






更新

如果要动态设置列(如我在评论中提到的那样),则必须设置 AutoGenerateColumns 更改为 False ,然后手动添加列集合。无需手动创建 DataGridTemplateColumns ,您可以在DataGrid的资源部分下声明它,并在后面的代码中使用它。

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>

后面的代码

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();
    }
}

这篇关于wpf用户控件的DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆