【问题标题】:Change the order of Columns in a DataGrid更改 DataGrid 中列的顺序
【发布时间】:2014-07-07 20:43:09
【问题描述】:

我想创建类似可重复使用的控件。

我已将一个名为 MyDataGrid.xaml 的页面添加到我的项目 (WPFApplication2) 的根目录中。我已将根元素从 Page 更改为 DataGrid 并向其添加一列。您可以在下面的 xaml 中看到变化:

<DataGrid x:Class="WpfApplication2.MyDataGrid"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Delete">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Button Content="X" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

在 MyDataGrid.xaml.cs 中,我将基类更改为 DataGrid。您可以在下面看到:

public partial class MyDataGrid : DataGrid
{
    public MyDataGrid()
    {
        InitializeComponent();
    }
}

现在我默认提供了 MainWindow.xaml。我将这个可重用控件添加到 MainWindow.xaml 并添加了一些列,如下所示:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:Self="clr-namespace:WpfApplication2"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Self:MyDataGrid>
            <Self:MyDataGrid.Columns>
                <DataGridTextColumn Header="Name" />
                <DataGridTextColumn Header="Age" />
                <DataGridTextColumn Header="Class" />
                <DataGridTextColumn Header="School" />
            </Self:MyDataGrid.Columns>
        </Self:MyDataGrid>
    </Grid>
</Window>

我的问题:

当我运行程序时,列的顺序如下:

删除姓名年龄班级学校

在上述列中,Delete Column 是默认提供的,其他列在 MainWindow.xmal 中声明

现在,我想更改列的顺序,以便默认列显示在 DataGrid 的末尾,如下所示:

姓名年龄班级学校删除

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    首先,这不是您创建可重用控件的方式。您应该定义一个仅继承 DataGrid 的类,并针对您的自定义控件使用 Styles

    您构建Reusable control 的方式将始终插入First Column 作为删除

    为了解决您的问题,我将Delete column 定义为资源。

    <DataGrid x:Class="WindowsApplication1.Blah"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Height="300" Width="300">
        <DataGrid.Resources>
            <DataGridTemplateColumn Header="Delete" x:Key="DeleteColumn">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Content="X" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" />
            <DataGridTextColumn Header="Age" />
            <DataGridTextColumn Header="Class" />
            <DataGridTextColumn Header="School" />
        </DataGrid.Columns>
    </DataGrid>
    

    然后在我的.cs 文件中,我把它变成了这样

    public partial class Blah : DataGrid
    {
        public Blah()
        {
            InitializeComponent();
            this.Loaded += OnLoaded;
        }
    
        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            var templateColumn = this.Resources["DeleteColumn"] as DataGridTemplateColumn;
            this.Columns.Add(templateColumn);
        }
    }
    

    现在总是在末尾添加Delete column

    【讨论】:

    • 谢谢。在我看了你的答案后,我以另一种方式做到了。但这个想法是一样的,我从你的回答中得到了这个想法。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2012-04-18
    • 1970-01-01
    • 2011-04-09
    • 1970-01-01
    相关资源
    最近更新 更多