首页 > 解决方案 > DataGrid 作为自定义 UserControl

问题描述

我正在尝试创建自己的 DataGrid,它可以像普通 DataGrid 一样构建,但具有一些额外的功能,例如过滤。

它应该采用 DataGridColumns fx 的类型。数据网格文本列。

构建我自己的原因是因为它将在多个视图中使用,但具有不同的列类型和不同的属性。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <local:MyDataGrid Grid.Row="0" GridItems="{Binding GetArtifactMdls}">
        <local:MyDataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}" />

        </local:MyDataGrid.Columns>
    </local:MyDataGrid>

    <DataGrid Grid.Row="1" ItemsSource="{Binding GetArtifactMdls}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Name}" />

        </DataGrid.Columns>
    </DataGrid>
</Grid>

如果有人可以指导我或创建一个快速样本,将不胜感激。

此致

标签: c#wpfdatagrid

解决方案


通过创建 DataGrid 固有的自定义控件并使用 DataGrid 模板解决了这个问题。通过右键单击设计视图中的 DataGrid 并选择编辑模板获取模板。


推荐阅读