样式或资源中的 WPF DataGrid 列 [英] WPF DataGrid Columns in Style or resource

查看:33
本文介绍了样式或资源中的 WPF DataGrid 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将 DataGrid 列声明为样式或资源?我想做这样的事情:

Is it possible to declare DataGrid columns in a style or as a resource? I would like to do something like this:

<....Resources>
    <DataGridColumns x:Key="dgcDataGridColumns">
        <DataGridTextColumn />
        <DataGridTextColumn />
    </DataGridColumns
</....Resources>

<DataGrid Columns="{StaticResource dgcDataGridColumns}" />

原因是我必须共享 4 个不同 DataGrids 的列定义.有什么方法可以实现这一目标吗?最好没有代码!

The reason is that i have to share the column definition for 4 different DataGrids. Any way to achieve this? Best would be without code behind!

推荐答案

DataGrid 的 Columns 属性没有设置器,因此不可能.但是,您可以执行以下操作:

Columns property of the DataGrid has no setter so it is not possibly. You can however do something like this:

<Window.Resources>

    <Controls:DataGrid x:Key="PersonDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding .}" x:Shared="False">
        <Controls:DataGrid.Columns>
            <Controls:DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}" IsReadOnly="True"/>
            <Controls:DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}" IsReadOnly="True"/>
        </Controls:DataGrid.Columns>
    </Controls:DataGrid>

</Window.Resources>

<StackPanel>
    <ContentControl Content="{StaticResource PersonDataGrid}" DataContext="{Binding Path=Customers}"></ContentControl>
    <ContentControl Content="{StaticResource PersonDataGrid}" DataContext="{Binding Path=Employees}"></ContentControl>
</StackPanel>

这篇关于样式或资源中的 WPF DataGrid 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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