DataGrid 支持字符截断显示

DataGrid支持截断时, 需要分2部分, DataGridColumnHeader和DataGridCell。
1)创建上述2部分的ControlTemplate 。
2)把其中的ContentPresenter改为TextBlock
3)使用TextTrimming属性

xaml code like this:


<ControlTemplate x:Key="DataGridCellControlTemplate" TargetType="{x:Type DataGridCell}">
<Border Background="{TemplateBinding Background}" SnapsToDevicePixels="True" BorderThickness="0" Height="{TemplateBinding Tag}">
            <TextBlock
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                HorizontalAlignment="Left"
                VerticalAlignment="Center"
                Margin="{TemplateBinding Padding}"
                Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}"
                TextTrimming="CharacterEllipsis"
                />
</Border>
</ControlTemplate>

<ControlTemplate x:Key="PublicDataGridColumnHeaderControlTemplate" TargetType="{x:Type DataGridColumnHeader}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="SortStates">
<VisualState x:Name="Unsorted"/>
<VisualState x:Name="SortAscending">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="path1">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="SortDescending">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="path">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
            <TextBlock
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                Margin="{TemplateBinding Padding}"
                Text="{TemplateBinding Content}"
                TextTrimming="CharacterEllipsis"
                utility:TextBlockTrimming.AutoTooltip="True"/>
<Thumb x:Name="PART_LeftHeaderGripper" HorizontalAlignment="Left" Style="{DynamicResource PublicDataGridHeaderLeftGripperStyle}" Margin="0,3" Visibility="{TemplateBinding SeparatorVisibility}"/>
<Thumb x:Name="PART_RightHeaderGripper" HorizontalAlignment="Right" Style="{DynamicResource PublicDataGridHeaderRightGripperStyle}" Margin="0,3" Background="#FF838282" Visibility="{TemplateBinding SeparatorVisibility}"/>
<Path x:Name="path" Data="M5.0000001,0 L10,7.0000002 0,7.0000002 z" Fill="Black" HorizontalAlignment="Right" Height="4" Margin="0,2,2,0" Stretch="Fill" Stroke="{x:Null}" VerticalAlignment="Top" Width="6" Visibility="Hidden"/>
<Path x:Name="path1" Data="M5,0 L10,6.9999999 0,6.9999999 z" Fill="Black" HorizontalAlignment="Right" Height="4" Margin="0,2,2,0" RenderTransformOrigin="0.5,0.499999995742525" Stretch="Fill" Stroke="{x:Null}" VerticalAlignment="Top" Width="6" Visibility="Hidden" >
<Path.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="-1" ScaleX="1"/>
<SkewTransform AngleY="0" AngleX="0"/>
<RotateTransform Angle="0"/>
<TranslateTransform/>
</TransformGroup>
</Path.RenderTransform>
</Path>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="SeparatorVisibility" Value="Hidden">
<Setter Property="Visibility" TargetName="PART_LeftHeaderGripper" Value="Hidden"/>
<Setter Property="Visibility" TargetName="PART_RightHeaderGripper" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
posted @ 2013-07-04 09:47  muzizongheng  阅读(488)  评论(0编辑  收藏  举报
如果我们时时忙着展现自己的知识, 将何从忆起成长所需的无知?