DataGrid列动态填充数据网格大小

时间:2014-01-17 15:05:39

标签: c# wpf datagrid

我的DataGrid horizontally stretched包含one column。问题是DataGrid得到了拉伸但列保持不变。我怎样才能填充DataGrid。所有这些都是动态完成的,如何使用代码完成。

var datagridDetails = new DataGrid
{
    AutoGenerateColumns = false,
    VerticalAlignment = VerticalAlignment.Top,
    HorizontalAlignment = HorizontalAlignment.Stretch
};

var columnDetails = new DataGridTextColumn
{
    Header = "Details",
    CanUserSort = false,
    CanUserResize = false,
    Binding = new Binding("Details"),
    Width = StackPanelContent.ActualWidth/2 - 8 //hacking through it but not working quite well
};

1 个答案:

答案 0 :(得分:1)

在XAML中,这将是:

<DataGrid ColumnWidth = "*"/>

<DataGridTextColumn Width="*" />

关于实际的列定义。

不确定如何在后面的代码中执行此操作。

编辑:

找到它,它将是:

var columnDetails = new DataGridTextColumn
{
    Header = "Details",
    CanUserSort = false,
    CanUserResize = false,
    Binding = new Binding("Details"),
    Width = new DataGridLength(1.0, DataGridLengthUnitType.Star)

};