如何在纯 XAML 的 DataGrid 端手动添加按钮?

How to add manually a button on a DataGrid's end in pure XAML?

我有一个包含一些列的 DataGrid:

<DataGrid.Columns>
    <DataGridTextColumn Header="Date" Binding="{Binding Date}, Mode=OneWay}"></DataGridTextColumn>
     //9 DataGridTextColumn more
</DataGrid.Columns>

我想在 DataGrid 的末尾添加一个按钮。无论有多少行,该按钮都应该是 "the last row",并且所有列上都有 colspan。我尝试了一些方法,但没有用:

//</DataGrid.Columns>
<local:MyRecord Date="" ....>
    <Button Grid.ColumnSpan="10" VerticalAlignment="Bottom" Click="Button1_Clicked" >Load More</Button>
</local:MyRecord>

背后的代码如下所示:

public class MyRecord{
    public string Date{ get; set;} 
    //...
}

如何 append/add 我的 DataGrid 末尾的按钮?纯 XAML 有可能吗?

首先,你需要使用一个ScrollViewer和一个StackPanel。将它们放在您的 DataGrid 周围:

<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
    <StackPanel>
        <DataGrid>
            ///Content goes here...
        </DataGrid>
        <Button Height="Auto" ....></Button>
    </StackPanel>
</ScrollViewer>

在您的 DataGrid 中,您需要 (DataGrid.)Template:

<DataGrid RowHeaderWidth="0">
    <DataGrid.Template>
        <ControlTemplate>
            <StackPanel Orientation="Vertical">
                <DataGridColumnHeadersPresenter />
                <ItemsPresenter />
        </ControlTemplate>
    </DataGrid.Template>
</DataGrid>

现在,DataGrid 使用 ScrollViewer 启用滚动。