【问题标题】:DataGrid column Fill to Datagrid size DynamicallyDataGrid 列动态填充到 Datagrid 大小
【发布时间】:2014-01-17 15:05:38
【问题描述】:

我有一个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
};

【问题讨论】:

    标签: c# wpf datagrid


    【解决方案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)
    

    };

    【讨论】:

    • 是的,我搜索过,XAML 方式是"*",但我没有找到代码方式
    • @ADi 我添加了一个编辑。应该是你要找的。​​span>
    • 也许是自动的 bcoz,它打开;y 提供与内容一样多的宽度。没用
    • @ADi 抱歉应该是 DataGridLengthUnitType.Star
    • (edit) 我的网格在滚动 vier 中,我删除了它,.Star 确实有效,但我需要 scoolviewr :(
    猜你喜欢
    • 2011-09-24
    • 2012-11-10
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多