DataGrid列未正确调整大小

时间:2012-07-29 07:17:05

标签: c# wpf xaml datagrid

我在WPF应用程序中使用DataGrid来显示一些数据。在应用程序加载时,有时Grid的列变得怪异,左侧的“Select All”按钮消失,导致列分隔符不在列的确切位置。只有当用户尝试调整列的大小时,网格的布局才会变好。

我的DataGrid定义是: <DataGrid Name="myList" AutoGenerateColumns="False" ItemsSource ="{Binding SortedView}" VirtualizingStackPanel.IsVirtualizing="False" ColumnWidth="Auto">

以下是问题的屏幕截图:

The bad layout of my DataGrid

以下是DataGrid的完整XAML代码:

<DataGrid Name="myList" AutoGenerateColumns="False" ItemsSource ="{Binding SortedView}" VirtualizingStackPanel.IsVirtualizing="False" ColumnWidth="Auto">
        <Interactivity:Interaction.Triggers>
            <Interactivity:EventTrigger EventName="Sorting" >
                <WPFCommon:InvokeDelegateCommandAction 
                            Command="{Binding SortCommand}"
                            CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=InvokeParameter}" />
            </Interactivity:EventTrigger>
        </Interactivity:Interaction.Triggers>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
            </Style>
        </DataGrid.RowStyle>
        <DataGrid.Columns>
            <DataGridTextColumn Header="{x:Static p:Resources.Sensor}" Binding="{Binding SensorName, Mode=OneWay}" Width="Auto" />
            <DataGridTextColumn Header="{x:Static p:Resources.Status}" CanUserSort="True" Binding="{Binding Classification, Mode=OneWay}" Width="Auto" />
            <DataGridTextColumn Header="{x:Static p:Resources.Created}" Binding="{Binding CreatedDate, Mode=OneWay}" CanUserSort="True" Width="Auto" IsReadOnly="True" />
            <DataGridTextColumn Header="{x:Static p:Resources.Last_Updated}" Binding="{Binding LastUpdated, Mode=OneWay}" CanUserSort="True" Width="*" IsReadOnly="True" />
        </DataGrid.Columns >
    </DataGrid>

如果有人知道如何让DataGrid在加载/更改时自动调整大小或刷新,那就太棒了。

谢谢!

0 个答案:

没有答案