DataGrid ColumnWidth Property ignored in DataTemplate for row details

╄→гoц情女王★ 提交于 2019-12-06 10:49:08

问题


This is the UserControl that displays the details from my application and as you can see the ColumnWidth Property is explicit set to *. I also tried to set the Width property from the DataGridTextColumn.

<UserControl x:Class="WpfUserInterface.MyDetailsView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Height="185" d:DesignWidth="480">
    <Grid>
        <DataGrid ColumnWidth="*" Margin="10">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Column1"/>
                <DataGridTextColumn Header="Column2"/>
                <DataGridTextColumn Header="Column3"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

This is the main window that only contains the DataGrid.

<Window x:Class="WpfUserInterface.Window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window" Height="306" Width="453">
    <Grid>
        <DataGrid ColumnWidth="*">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ParentColumn1"/>
                <DataGridTextColumn Header="ParentColumn2"/>
                <DataGridTextColumn Header="ParentColumn3"/>
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <MyDetailsView/>
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </Grid>
</Window>

This is what shows up on the screen when you run the application and select a row in the parent DataGrid.

When I set the width of the DataGrid in the MyDetailsView to a specified value like 400 the columns are sized perfect but this is not an option. Is there any way to solve this problem? A workaround?


回答1:


I know this question was asked about a year ago, but I've been facing the same problem and I've found out this solution:

 this.dgrData.Columns[0].Width = new DataGridLength(1, DataGridLengthUnitType.Star);
 this.dgrData.Columns[1].Width = new DataGridLength(1, DataGridLengthUnitType.Star);
 ...

Hope it may be of any help to someone.



来源:https://stackoverflow.com/questions/12758886/datagrid-columnwidth-property-ignored-in-datatemplate-for-row-details

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!