我最终在我的网格中使用了自定义控件:
<telerik:GridViewDataColumn UniqueName="TaskDetails"
DataMemberBinding="{Binding TaskDetails}"
Header="Task details"
Width="500"
IsReadOnly="True">
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<cc:LongTextBlock Text="{Binding TaskDetails}" ToolTipShowDuration="300000" VerticalAlignment="Center" ToolTipWidth="350" />
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
LongTextBlock 是具有 3 个依赖属性的简单控件:
public class LongTextBlock : Control
{
static LongTextBlock()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LongTextBlock), new FrameworkPropertyMetadata(typeof(LongTextBlock)));
}
#region DependencyProperty Text of LongTextBlock
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(LongTextBlock),
new UIPropertyMetadata(string.Empty));
#endregion
#region DependencyProperty ToolTipShowDuration of LongTextBlock
public int ToolTipShowDuration
{
get { return (int)GetValue(ToolTipShowDurationProperty); }
set { SetValue(ToolTipShowDurationProperty, value); }
}
public static readonly DependencyProperty ToolTipShowDurationProperty =
DependencyProperty.Register("ToolTipShowDuration", typeof(int), typeof(LongTextBlock),
new UIPropertyMetadata(10000));
#endregion
#region DependencyProperty ToolTipWidth of LongTextBlock
public double ToolTipWidth
{
get { return (double)GetValue(ToolTipWidthProperty); }
set { SetValue(ToolTipWidthProperty, value); }
}
public static readonly DependencyProperty ToolTipWidthProperty =
DependencyProperty.Register("ToolTipWidth", typeof(double), typeof(LongTextBlock),
new UIPropertyMetadata(350.0));
#endregion
}
和控件的样式:
<Style TargetType="{x:Type local:LongTextBlock}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LongTextBlock}">
<TextBlock Text="{TemplateBinding Text}" TextTrimming="WordEllipsis" ToolTipService.ShowDuration="{TemplateBinding ToolTipShowDuration}">
<TextBlock.ToolTip>
<ToolTip>
<TextBlock Width="{TemplateBinding ToolTipWidth}" TextWrapping="WrapWithOverflow" Text="{TemplateBinding Text}" />
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>