2017-06-24 45 views
-1

我創建了一個「可綁定」TextBlock,即綁定到項目的文本塊,並且這些項目具有定義應如何呈現文本的屬性(color,for例)。TextTrimming屬性不適用於自定義TextBlock

然而,'TextTrimming`屬性不爲我的自定義文本塊的工作,下面的代碼:

class BindableTextBlock : TextBlock 
{ 
    public BindableTextBlock() 
    { 

    } 

    public bool HideWhenEmpty 
    { 
     get { return (bool)GetValue(HideWhenEmptyProperty); } 
     set { SetValue(HideWhenEmptyProperty, value); } 
    } 

    public static readonly DependencyProperty HideWhenEmptyProperty = 
     DependencyProperty.Register("HideWhenEmpty", typeof(bool), typeof(BindableTextBlock), new UIPropertyMetadata(false)); 

    public ObservableCollection<DescriptionToken> Items 
    { 
     get { return (ObservableCollection<DescriptionToken>)GetValue(ItemsProperty); } 
     set { SetValue(ItemsProperty, value); } 
    } 

    public static readonly DependencyProperty ItemsProperty = 
     DependencyProperty.Register("Items", typeof(ObservableCollection<DescriptionToken>), typeof(BindableTextBlock), new UIPropertyMetadata(null, OnItemsPropertyChanged)); 

    private static void OnItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     ((BindableTextBlock)d).OnItemsChanged(); 
    } 

    private void OnItemsChanged() 
    { 
     if (Items == null) 
      return; 
     Items.CollectionChanged -= ItemsCollectionChanged; 
     Items.CollectionChanged += ItemsCollectionChanged; 

     BuildText(); 
    } 

    private void BuildText() 
    { 
     Inlines.Clear(); 
     if (Items == null || !Items.Any()) 
     { 
      if (HideWhenEmpty) 
       Visibility = System.Windows.Visibility.Collapsed; 
      return; 
     } 

     for (var i = 0; i < Items.Count; i++) 
     { 
      var item = Items[i]; 
      var run = new Run(TruncateText(item.Text)); 
      if (item.IsVariable) 
      { 
       run.Foreground = new SolidColorBrush(item.IsError ? Colors.Red : Colors.Blue); 
       run.FontStyle = FontStyles.Italic; 
      } 
      Inlines.Add(run); 
     } 

     Visibility = System.Windows.Visibility.Visible; 
     InvalidateVisual(); 
    } 

    private string TruncateText(string text) 
    { 
     if (string.IsNullOrEmpty(text)) 
      return text; 

     if (text.Contains(Environment.NewLine)) 
      return TruncateText(text.Substring(0, text.IndexOf(Environment.NewLine)) + "..."); 

     if (text.Length > 120) 
     { 
      var result = text.Substring(0, 120).TrimEnd('.'); 
      return result + "..."; 
     } 

     return text; 
    } 

    private void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     BuildText(); 
    } 

每當Items改變,它操縱TextBlock.Inlines以設置文本顏色。

XAML

<DataTemplate> 
<Border Padding="3"> 
      <StackPanel Grid.Column="1" Orientation="Vertical"> 

       <TextBlock Text="aaaaaaa aaaaaaaaaa aaaaaaaaaaaaa bbbbbb cccccccccc ddddddddddd eeeeeeeeeeeeeeee ffffffffffffffffff ggggggggggg" FontStyle="Italic" FontSize="10" TextTrimming="CharacterEllipsis"/> 
       <controls:BindableTextBlock Items="{Binding Description}" FontSize="10" 
              FontStyle="Italic" HideWhenEmpty="True" 
              TextTrimming="CharacterEllipsis"/> 
      </StackPanel> 
     </Border> 
     </DataTemplate> 

下圖顯示的是第一TextBlock修剪,但第二個(我)不: enter image description here

如何使我的自定義TextBlock工作就像有關TextTrimming屬性的常規TextBlock確實?

+0

爲什麼downvote ??? – JobaDiniz

回答

0

如果您的控制包裹,它不能修剪。 因此請刪除下列代碼:

TextWrapping = System.Windows.TextWrapping.Wrap; 

from your control的構造函數。

+0

我稍後刪除它(並忘記更新問題)..它也不起作用 – JobaDiniz

+0

我發誓我已經測試過,但是,它是'TextWrapping'。謝謝 – JobaDiniz

+0

沒問題,歡迎@JobaDiniz –

相關問題