2012-09-26 58 views
0

我將telerik網格控件綁定到ViewModel。其中一個屬性可能包含相對較長的字符串,難以適應網格。所以我顯示單元與的TextTrimming =「WordEllipsis」一個TextBlock並顯示在工具提示控制的全文:當網格排序時工具提示綁定搞砸了

<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

最後我用我的網格自定義控件:

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

在你的情況,你可以嘗試把文本框的用戶控件,或處理ToolTipOpening事件以另一種方式,或者明確地結合工具提示的的DataContext到TextBlock的DataContext的