DataGrid列动态填充数据网格大小(DataGrid column Fill to Datagrid size Dynamically)

编程入门 行业动态 更新时间:2024-10-27 21:23:24
DataGrid列动态填充数据网格大小(DataGrid column Fill to Datagrid size Dynamically)

我有一个horizontally stretched的DataGrid包含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 };

I have a DataGrid which is horizontally stretched containing one column. the problem is the DataGrid gets streached but the column remains same. How can i make it fill the DataGrid. All is done Dynamically, how to do it with code.

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 };

最满意答案

在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)

};

In XAML this would be:

<DataGrid ColumnWidth = "*"/>

or

<DataGridTextColumn Width="*" />

On the actual column definition.

Not sure how to do this in code behind.

Edit:

Found it, it would be:

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

};

更多推荐

DataGrid,new,var,column,false,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-07-24 17:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1248862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   大小   动态   数据   DataGrid

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!