c# – TextBlock,其中一些文本左对齐,其余文本右对齐

前端之家收集整理的这篇文章主要介绍了c# – TextBlock,其中一些文本左对齐,其余文本右对齐前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想要一个包含以下文本的文本块:
  1. My associated textBox is :

文本左对齐,冒号右对齐.

我知道如何使用两个文本块获得上述输出.但我想知道适用于单个文本块的行为是否相同?

解决方法

TextBlock本质上反对对齐孩子的概念,但当然有一些可能的黑客攻击:

>填写空格以给出对齐的外观.
>使用InlineUIContainer在TextBlock中添加实际的UI元素(可以对齐).

我将通过创建具有LeftAlignedText和RightAlignedText属性的ExtendedTextBlock控件给出每个示例.用法是这样的:

  1. <my:ExtendedTextBlock RightAlignedText=":" LeftAlignedText="My associated textBox is" />

1)用空格填充.

对于这种方法,我从this answer借来获得文本字符串的实际宽度.这个想法基本上是从控件的实际宽度中减去文本的总宽度,并在它们之间插入适当数量的空格.

  1. public class ExtendedTextBlock : TextBlock
  2. {
  3. public string RightAlignedText
  4. {
  5. get { return (string)GetValue(RightAlignedTextProperty); }
  6. set { SetValue(RightAlignedTextProperty,value); }
  7. }
  8. public static readonly DependencyProperty RightAlignedTextProperty =
  9. DependencyProperty.Register("RightAlignedText",typeof(string),typeof(ExtendedTextBlock),new PropertyMetadata(SetText));
  10.  
  11. public string LeftAlignedText
  12. {
  13. get { return (string)GetValue(LeftAlignedTextProperty); }
  14. set { SetValue(LeftAlignedTextProperty,value); }
  15. }
  16. public static readonly DependencyProperty LeftAlignedTextProperty =
  17. DependencyProperty.Register("LeftAlignedText",new PropertyMetadata(SetText));
  18.  
  19. public static void SetText(object sender,DependencyPropertyChangedEventArgs args)
  20. {
  21. SetText((ExtendedTextBlock)sender);
  22. }
  23.  
  24. public static void SetText(ExtendedTextBlock owner)
  25. {
  26. if (owner.ActualWidth == 0)
  27. return;
  28.  
  29. // helper function to get the width of a text string
  30. Func<string,double> getTextWidth = text =>
  31. {
  32. var formattedText = new FormattedText(text,CultureInfo.CurrentUICulture,FlowDirection.LeftToRight,new Typeface(owner.FontFamily,owner.FontStyle,owner.FontWeight,owner.FontStretch),owner.FontSize,Brushes.Black);
  33. return formattedText.Width;
  34. };
  35.  
  36. // calculate the space needed to fill in
  37. double spaceNeeded = owner.ActualWidth - getTextWidth(owner.LeftAlignedText ?? "") - getTextWidth(owner.RightAlignedText ?? "");
  38.  
  39. // get the width of an empty space (have to cheat a bit since the width of an empty space returns zero)
  40. double spaceWidth = getTextWidth(" .") - getTextWidth(".");
  41. int spaces = (int)Math.Round(spaceNeeded / spaceWidth);
  42.  
  43. owner.Text = owner.LeftAlignedText + new string(Enumerable.Repeat(' ',spaces).ToArray()) + owner.RightAlignedText;
  44. }
  45.  
  46. public ExtendedTextBlock()
  47. {
  48. SizeChanged += (sender,args) => SetText(this);
  49. }
  50. }

2)使用InlineUIContainer添加对齐的文本

这里的想法是在TextBlock中添加一个面板,它将负责对齐每个文本字符串.这是基本的想法:

  1. <TextBlock>
  2. <InlineUIContainer>
  3. <Grid Width="{Binding RelativeSource={RelativeSource AncestorType=TextBlock},Path=ActualWidth}">
  4. <TextBlock Text="Hello" />
  5. <TextBlock Text="World" TextAlignment="Right" />
  6. </Grid>
  7. </InlineUIContainer>
  8. </TextBlock>

因此,此版本的控件重新创建上述内容但隐藏了实现.它将InlineUIContainer控件添加到基本TextBlock,并保持对“左”和“右”TextBlocks的引用,根据需要设置其文本.

  1. public class ExtendedTextBlock2 : TextBlock
  2. {
  3. private TextBlock _left,_right;
  4.  
  5. public string RightAlignedText
  6. {
  7. get { return (string)GetValue(RightAlignedTextProperty); }
  8. set { SetValue(RightAlignedTextProperty,typeof(ExtendedTextBlock2),new PropertyMetadata(SetText));
  9.  
  10. public static void SetText(object sender,DependencyPropertyChangedEventArgs args)
  11. {
  12. ((ExtendedTextBlock2)sender).SetText();
  13. }
  14.  
  15. public void SetText()
  16. {
  17. if (_left == null || _right == null)
  18. return;
  19. _left.Text = LeftAlignedText;
  20. _right.Text = RightAlignedText;
  21. }
  22.  
  23. public ExtendedTextBlock2()
  24. {
  25. Loaded += ExtendedTextBlock2_Loaded;
  26. }
  27.  
  28. void ExtendedTextBlock2_Loaded(object sender,RoutedEventArgs e)
  29. {
  30. Inlines.Clear();
  31. var child = new InlineUIContainer();
  32. var container = new Grid();
  33. var widthBinding = new Binding { Source = this,Path = new PropertyPath(TextBlock.ActualWidthProperty) };
  34. container.SetBinding(Grid.WidthProperty,widthBinding);
  35. child.Child = container;
  36. container.Children.Add(_left = new TextBlock { HorizontalAlignment = System.Windows.HorizontalAlignment.Left });
  37. container.Children.Add(_right = new TextBlock { HorizontalAlignment = System.Windows.HorizontalAlignment.Right });
  38. Inlines.Add(child);
  39.  
  40. SetText();
  41. }
  42. }

猜你在找的C#相关文章