【问题标题】:ToolTips binding messed up when grid is sorted对网格进行排序时,工具提示绑定搞砸了
【发布时间】:2012-09-18 12:28:43
【问题描述】:

我将 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 会显示正确的数据,但工具提示会显示先前位于该位置的单元格的文本。就像它没有被排序一样。

知道如何解决这个问题吗?

【问题讨论】:

    标签: wpf data-binding binding tooltip telerik-grid


    【解决方案1】:

    我最终在我的网格中使用了自定义控件:

    <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>
    

    【讨论】:

      【解决方案2】:

      我们遇到了类似的问题,但使用了不同的网格。 数据模板包含一个用户控件,在里面我们有这个:

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多