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?
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