c# - 在 WPF DataGrid 中均匀分布列

标签 c# wpf datagrid

我将窗口设置为 SizeToContent="WidthAndHeight" 并且在我的窗口中有一排我想要定义窗口宽度的控件。在这些控件下面,我想要一个包含三列的 DataGrid。

但是,我似乎无法均匀分布三列,使它们具有相同的大小并使用可用空间。有没有办法在 C#.NET 中做到这一点?

最佳答案

您可以通过数据绑定(bind)来完成此操作。例如:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" SizeToContent="WidthAndHeight">
  <StackPanel Orientation="Vertical">
    <StackPanel Orientation="Horizontal" x:Name="panel">
        <Label Content="1" Width="90"/>
        <Label Content="2" Width="90"/>
        <Label Content="3" Width="90"/>
    </StackPanel>

    <DataGrid Width="{Binding ActualWidth, ElementName=panel}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="1" Width="*"/>
            <DataGridTextColumn Header="2" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>
  </StackPanel>
</Window>

关于c# - 在 WPF DataGrid 中均匀分布列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17806688/

相关文章:

c# - 解释 C# 中的语句

wpf - 依赖属性没有更新我的用户控件

wpf - 调整一个行高时如何设置wpf数据网格的所有行高

c# - 如何从 View 模型访问不可绑定(bind)的 View 属性?

GWT 如何将自定义单元格添加到单元格/数据网格

javascript - 无法保存到 Dojo 中

带图像上传的 C# 多部分表单帖子

c# - 异步友好的 DispatcherTimer 包装器/子类

c# - 如何在asp.net core中设置linux nlog文件夹

c# - 如何在 Bootstrap 类仍在运行时关闭应用程序?