我将telerik网格控件绑定到ViewModel。其中一个属性可以包含相对较长的字符串,很难适应网格。所以我用TextTrimming =“WordEllipsis”将单元格显示为TextBlock,并在ToolTip控件中显示全文:
<telerik:GridViewDataColumn UniqueName="TaskDetails" DataMemberBinding="{Binding TaskDetails}" Header="Task details" IsReadOnly="True" Width="*" >
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding TaskDetails}" ToolTipService.ShowDuration="300000" TextTrimming="WordEllipsis" Height="30">
<TextBlock.ToolTip>
<ToolTip>
<TextBlock Width="350" TextWrapping="WrapWithOverflow" Text="{Binding TaskDetails}" />
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
在用户对网格进行排序之前,一切正常。如果应用的排序TextBlock显示正确的数据,但工具提示显示以前位于该位置的单元格的文本。就像没有排序一样。
知道如何解决这个问题吗?
答案 0 :(得分:0)
虽然网格不同,但我们遇到了类似的问题。 datatemplate包含一个用户控件,在里面我们有这个:
public WebSnapshotCell()
{
InitializeComponent();
this.ToolTipOpening += new ToolTipEventHandler(WebSnapshotCell_ToolTipOpening);
}
void WebSnapshotCell_ToolTipOpening(object sender, ToolTipEventArgs e)
{
// Refresh tooltip's data context
ToolTip tooltip = this.ToolTip as ToolTip;
if (tooltip != null)
{
tooltip.DataContext = this.DataContext;
}
}
在您的情况下,您可以尝试将TextBox放在UserControl中,或以另一种方式处理ToolTipOpening事件,或者将ToolTip的DataContext显式绑定到TextBlock的DataContext
答案 1 :(得分:0)
我最终在我的网格中使用自定义控件:
<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>