WPF 数据网格根据最宽"row"设置宽度



我的 DataGrid 有问题。列根据最宽的可见单元格加载,因此,如果用户向下滚动并显示更宽的单元格,则会调整数据网格的大小以适应新的单元格宽度。

我的用户不喜欢这种情况,所以我寻找了一种修复此行为的方法。我发现的一个简单的建议是将列宽设置为最宽的单元格宽度。但是我这样做有些麻烦。

如果您对该解决方案有建议或有其他解决方案来解决我的问题,那就太好了。

<Window x:Class="Win"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        SizeToContent="Width">        
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <GroupBox Grid.Row="0" Header="Seach">
            <TextBox TextChanged="TextBoxFilter_TextChanged"/>
        </GroupBox>
        <DataGrid Grid.Row="1" Name="DataGridOperations" AutoGenerateColumns="False" ColumnWidth="auto" CanUserAddRows="False" CanUserDeleteRows="False" AlternatingRowBackground="AntiqueWhite">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Date" IsReadOnly="True" Binding="{Binding Date,Mode=OneTime, StringFormat={}{0:dd/MM/yyyy hh:mm:ss}}"/>
                <DataGridTextColumn Header="Type" IsReadOnly="True" Binding="{Binding Type,Mode=OneTime}"/>
                <DataGridTextColumn Header="Etat" IsReadOnly="True" Binding="{Binding Status,Mode=OneTime}"/>
                <DataGridTextColumn Header="Name" IsReadOnly="True" Binding="{Binding Name,Mode=OneTime}"/>
                <DataGridTextColumn Header="Code" Binding="{Binding CodeProduct,Mode=OneTime}"/>
                <DataGridTextColumn Header="Version" IsReadOnly="True" Binding="{Binding VersionIndus,Mode=OneTime}"/>
            </DataGrid.Columns>
        </DataGrid>    
    </Grid>
</Window>

忽略筛选器模块。他在加载步骤上被禁用。我使用 C# 4.0 .NET。

如果在 XAML 中将列宽设置为 *,该怎么办:

 Width="*"

并创建一个允许文本换行的单元格模板(可能为此使用 texblock )这样列宽保持不变干杯,G

最新更新