2017-06-24 2 views
-1

J'ai créé un "bindable" TextBlock, c'est-à-dire un bloc de texte lié à des éléments, et ces éléments ont des propriétés qui définissent la façon dont le texte doit être rendu (couleur, Exemple).La propriété TextTrimming ne fonctionne pas pour un TextBlock personnalisé

Cependant, la propriété'TextTrimming` ne fonctionne pas pour mon textblock personnalisé, voici le code:

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

Chaque fois que Items changé, il manipule TextBlock.Inlines afin de définir la couleur du texte.

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> 

L'image suivante montre qui est rognée le 1er TextBlock, mais le 2ème (le mien) n'est pas: enter image description here

Comment faire mon travail sur mesure TextBlock comme la régulière TextBlock fait concernant TextTrimming propriété?

+0

pourquoi downvote ??? – JobaDiniz

Répondre

0

Si votre contrôle enveloppe, il ne peut pas couper. enlever Donc, ce code:

TextWrapping = System.Windows.TextWrapping.Wrap; 

du constructeur de votre contrôle.

+0

Je l'ai enlevé plus tard (et j'ai oublié de mettre à jour la question) .. ça ne marche pas non plus – JobaDiniz

+0

Je jure que j'ai déjà testé ça, mais oui, c'était "TextWrapping". Merci – JobaDiniz

+0

Pas de problème, vous êtes les bienvenus @JobaDiniz –