WPF DataGrid文本被截断

时间:2015-06-30 14:33:53

标签: wpf xaml datagrid

我的WPF 4.5应用程序有一个小的(但很烦人的)可视错误,其中DataGrid的单元格在加载时被切断: initial load Dropbox link to the screenshot

但是一旦你调整窗口大小(单击右上角的方形按钮)并重新最大化它,DataGrid的单元格就会出现: after resizing Dropbox link to the screenshot

在XAML中,每个列的宽度都设置为Auto,“名称”列除外,*。我有什么办法可以阻止这种视觉错误的发生吗/有什么我正在做的导致这个错误吗?

这是我的XAML:

<DataGrid ItemsSource="{Binding Datasets, NotifyOnTargetUpdated=True}" Name="dsDatagrid" SelectionMode="Extended" MouseDoubleClick="ViewDataset">
<DataGrid.Style>
    <Style BasedOn="{StaticResource {x:Type DataGrid}}" TargetType="DataGrid">
        <Setter Property="Visibility" Value="Visible"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsWorking}" Value="True">
                <Setter Property="Visibility" Value="Collapsed"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Style>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="TargetUpdated">
        <cmd:EventToCommand Command="{Binding CollectionChangedCommand}"/>
    </i:EventTrigger>
    <i:EventTrigger EventName="SelectionChanged">
        <cmd:EventToCommand Command="{Binding SelectionChangedCommand}" CommandParameter="{Binding ElementName=dsDatagrid, Path=SelectedItems}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
    <DataGridTemplateColumn Header="" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Name="NameCell" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Center">
                    <Button Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.BackupSingleCommand}" CommandParameter="{Binding}" Style="{StaticResource BackupSingleButtonStyle}" Margin="5 10"/>
                    <Button Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.DeleteSingleCommand}" CommandParameter="{Binding}" Style="{StaticResource DeleteSingleButtonStyle}" Margin="5 10"/>
                </StackPanel>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTextColumn Header="ID" Width="Auto" Binding="{Binding Id}"/>
    <DataGridTemplateColumn Header="Name" Width="*">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" TextWrapping="Wrap"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTextColumn Header="Source Images" Width="Auto" Binding="{Binding SourceImages.Count, StringFormat={}{0:N0}, TargetNullValue=NONE}"/>
    <DataGridTextColumn Header="Start Time" Width="Auto" Binding="{Binding StartTime, StringFormat={}{0:MM}/{0:dd}/{0:yy} {0:HH}\:{0:mm}\:{0:ss}}"/>
    <DataGridTextColumn Header="End Time" Width="Auto" Binding="{Binding EndTime, StringFormat={}{0:MM}/{0:dd}/{0:yy} {0:HH}\:{0:mm}\:{0:ss}, TargetNullValue=In Progress}"/>
    <DataGridTemplateColumn Header="Status" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Status}" TextWrapping="WrapWithOverflow"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

如果相关,这是我的DataGrid样式:

<Style TargetType="DataGridColumnHeader">
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Background" Value="{StaticResource BannerNormalBrush}"/>
    <Setter Property="Padding" Value="24 10"/>
</Style>
<Style TargetType="DataGridRow">
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="FontSize" Value="16"/>
</Style>
<Style TargetType="DataGridCell">
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Padding" Value="24 0"/>
    <Setter Property="Foreground" Value="{StaticResource TextBlockDisabledForegroundBrushDark}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="DataGridCell">
                <Border Name="Border" Padding="{TemplateBinding Padding}" Background="Transparent">
                    <ContentPresenter VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{StaticResource DataGridRowSelectedBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style TargetType="DataGrid">
    <Setter Property="GridLinesVisibility" Value="None"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="RowBackground" Value="{StaticResource DataGridOddRowBackgroundBrush}"/>
    <Setter Property="AlternatingRowBackground" Value="{StaticResource DataGridEvenRowBackgroundBrush}"/>
    <Setter Property="CanUserResizeRows" Value="False"/>
    <Setter Property="CanUserResizeColumns" Value="False"/>
    <Setter Property="IsReadOnly" Value="True"/>
    <Setter Property="Margin" Value="5"/>
    <Setter Property="SelectionMode" Value="Single"/>
    <Setter Property="RowHeaderWidth" Value="0"/>
    <Setter Property="CanUserReorderColumns" Value="False"/>
    <Setter Property="CanUserSortColumns" Value="False"/>
    <Setter Property="AutoGenerateColumns" Value="False"/>
    <Setter Property="VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="HorizontalScrollBarVisibility" Value="Auto"/>
    <Setter Property="EnableRowVirtualization" Value="True"/>
    <Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
    <Setter Property="VirtualizingStackPanel.VirtualizationMode" Value="Recycling"/>
    <Setter Property="Background" Value="Transparent"/>
</Style>

1 个答案:

答案 0 :(得分:1)

还没有真正尝试过您的代码,但仅仅通过查看它,我就有了预感......

StackPanels在计算可用空间时往往会遇到问题,因此您的问题可能是由列的DataTemplate中的StackPanel引起的,无法在加载时将其大小正确地传递给列。

我的建议是更改另一个Panel的StackPanel。在这种情况下,一个好的旧网格可以正常工作,我打赌你这次不会看到任何故障。

<DataGridTemplateColumn Header="" Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Grid Name="NameCell" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Button Grid.Column="0" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.BackupSingleCommand}" CommandParameter="{Binding}" Style="{StaticResource BackupSingleButtonStyle}" Margin="5 10"/>
                <Button Grid.Column="1" Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=DataContext.DeleteSingleCommand}" CommandParameter="{Binding}" Style="{StaticResource DeleteSingleButtonStyle}" Margin="5 10"/>
            </StackPanel>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
相关问题