DataTemplate中忽略的DataGrid列宽度属性,以获取行细节

时间:2021-07-09 08:53:34

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,您可以看到ColumnWidth属性被显式设置为*。我还尝试从DataGridTextColumn设置Width属性。

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

这是只包含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.

这是在运行应用程序并在父DataGrid中选择一行时显示在屏幕上的内容。

DataTemplate中忽略的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?

当我将MyDetailsView中的DataGrid的宽度设置为指定值(比如400)时,列的大小是完美的,但这不是一个选项。有办法解决这个问题吗?一个解决方法吗?

1 个解决方案

#1


0  

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.

希望这对某人有所帮助。

#1


0  

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.

希望这对某人有所帮助。