栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

WPF 分散对齐TextBlock

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WPF 分散对齐TextBlock

用了一个UserControl实现(可以设置文字对齐方式外,单独提供一个bool量控制是否分散分散,分散对齐情况下HorizontalContentAlignment和VerticalContentAlignment失效)

内部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test
{
    /// 
    /// UserControl1.xaml 的交互逻辑
    /// 
    public partial class UTextBlock : UserControl
    {
        public UTextBlock()
        {
            InitializeComponent();
        }

        public TextDecorationCollectionTextDecorations
        {
            get { return (TextDecorationCollection)GetValue(TextDecorationsProperty); }
            set { SetValue(TextDecorationsProperty, value); }
        }

        public static readonly DependencyProperty TextDecorationsProperty =
            DependencyProperty.Register(nameof(TextDecorationCollection), typeof(TextDecoration), typeof(UTextBlock), new PropertyMetadata(new TextDecorationCollection()));


        new public FontStyle FontStyle
        {
            get { return (FontStyle)GetValue(FontStyleProperty); }
            set { SetValue(FontStyleProperty, value); }
        }

        new public static readonly DependencyProperty FontStyleProperty =
            DependencyProperty.Register(nameof(FontStyle), typeof(FontStyle), typeof(UTextBlock), new PropertyMetadata(FontStyles.Normal));



        new public FontWeight FontWeight
        {
            get { return (FontWeight)GetValue(FontWeightProperty); }
            set { SetValue(FontWeightProperty, value); }
        }

        new public static readonly DependencyProperty FontWeightProperty =
            DependencyProperty.Register(nameof(FontWeight), typeof(FontWeight), typeof(UTextBlock), new PropertyMetadata(FontWeights.Normal));



        new public double FontSize
        {
            get { return (double)GetValue(FontSizeProperty); }
            set { SetValue(FontSizeProperty, value); }
        }

        new public static readonly DependencyProperty FontSizeProperty =
            DependencyProperty.Register(nameof(FontSize), typeof(double), typeof(UTextBlock), new PropertyMetadata(19d, (d, e) =>
            {
                (d as UTextBlock).getFontStringSize();
            }));


        new public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }

        new public static readonly DependencyProperty ForegroundProperty =
            DependencyProperty.Register(nameof(Foreground), typeof(Brush), typeof(UTextBlock), new PropertyMetadata(Brushes.Black));

        new public Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        new public static readonly DependencyProperty BackgroundProperty =
            DependencyProperty.Register(nameof(Background), typeof(Brush), typeof(UTextBlock), new PropertyMetadata(Brushes.Transparent));


        new public FontFamily FontFamily
        {
            get { return (FontFamily)GetValue(FontFamilyProperty); }
            set { SetValue(FontFamilyProperty, value); }
        }

        new public static readonly DependencyProperty FontFamilyProperty =
            DependencyProperty.Register(nameof(FontFamily), typeof(FontFamily), typeof(UTextBlock), new PropertyMetadata(new FontFamily("Calibri")));

        new public System.Windows.HorizontalAlignment HorizontalContentAlignment
        {
            get { return (System.Windows.HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty); }
            set { SetValue(HorizontalContentAlignmentProperty, value); }
        }

        new public static readonly DependencyProperty HorizontalContentAlignmentProperty =
            DependencyProperty.Register(nameof(HorizontalContentAlignment), typeof(System.Windows.HorizontalAlignment), typeof(UTextBlock), new PropertyMetadata(System.Windows.HorizontalAlignment.Left));

        new public System.Windows.VerticalAlignment VerticalContentAlignment
        {
            get { return (System.Windows.VerticalAlignment)GetValue(VerticalContentAlignmentProperty); }
            set { SetValue(VerticalContentAlignmentProperty, value); }
        }

        new public static readonly DependencyProperty VerticalContentAlignmentProperty =
            DependencyProperty.Register(nameof(VerticalContentAlignment), typeof(System.Windows.VerticalAlignment), typeof(UTextBlock), new PropertyMetadata(System.Windows.VerticalAlignment.Top));


        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        public static readonly DependencyProperty TextProperty =
             DependencyProperty.Register(nameof(Text), typeof(string), typeof(UTextBlock), new PropertyMetadata(string.Empty, (d, e) =>
             {
                 (d as UTextBlock).setSplitAlignment();
             }));

        public static readonly DependencyProperty isFensanduiqiProperty =
             DependencyProperty.Register(nameof(IsFenSanDuiQi), typeof(bool), typeof(UTextBlock), new PropertyMetadata(false, (d, e) =>
             {
                 (d as UTextBlock).setSplitAlignment();
             }));
        public bool IsFenSanDuiQi
        {
            get { return (bool)GetValue(isFensanduiqiProperty); }
            set { SetValue(isFensanduiqiProperty, value); }
        }

        //设置分散对齐
        private void setSplitAlignment()
        {
            grid1.Children.Clear();
            grid1.ColumnDefinitions.Clear();
            if (IsFenSanDuiQi)
            {
                for (int i = 0; i < Text.Length; i++)
                {
                    ColumnDefinition cd = new ColumnDefinition();
                    grid1.ColumnDefinitions.Add(cd);
                }
                char[] ch = Text.ToCharArray();
                for (int i = 0; i < Text.Length; i++)
                {
                    TextBlock tb = new TextBlock();
                    tb.Text = ch[i].ToString();
                    //if (i == 0)
                    //{
                    //    tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    //}
                    //else if (i == Text.Length - 1)
                    //{
                    //    tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                    //}
                    //else
                    //{
                        tb.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    //}

                    setBinding(tb);
                    tb.SetValue(Grid.ColumnProperty, i);
                    grid1.Children.Add(tb);

                }
            }
            else
            {
                TextBlock tb = new TextBlock();
                tb.Text = Text;
                Binding binding = new Binding(nameof(HorizontalContentAlignment)) { Source = this };
                tb.SetBinding(TextBlock.HorizontalAlignmentProperty, binding);
                Binding binding2 = new Binding(nameof(VerticalContentAlignment)) { Source = this };
                tb.SetBinding(TextBlock.VerticalAlignmentProperty, binding2);
                setBinding(tb);
                grid1.Children.Add(tb);
            }

            getFontStringSize();
        }

        private void setBinding(TextBlock tb)
        {
            Binding binding = new Binding(nameof(FontSize)) { Source = this };
            tb.SetBinding(TextBlock.FontSizeProperty, binding);
            Binding binding2 = new Binding(nameof(Foreground)) { Source = this };
            tb.SetBinding(TextBlock.ForegroundProperty, binding2);
            Binding binding3 = new Binding(nameof(FontFamily)) { Source = this };
            tb.SetBinding(TextBlock.FontFamilyProperty, binding3);
            Binding binding4 = new Binding(nameof(FontWeight)) { Source = this };
            tb.SetBinding(TextBlock.FontWeightProperty, binding4);
            Binding binding5 = new Binding(nameof(FontStyle)) { Source = this };
            tb.SetBinding(TextBlock.FontStyleProperty, binding5);
            Binding binding6 = new Binding(nameof(TextDecorations)) { Source = this };
            tb.SetBinding(TextBlock.TextDecorationsProperty, binding6);
        }

        private void getFontStringSize()
        {
            FormattedText MyFormattedText = new FormattedText(Text, //文本描述
                                                         System.Globalization.CultureInfo.GetCultureInfo(86),
                                                         FlowDirection.RightToLeft,
                                                         new Typeface(FontFamily, FontStyle, FontWeight, FontStretch),//字体类型
                                                         FontSize,//字体大小
                                                         Foreground);
            this.Height = MyFormattedText.Height;
            MaxHeight = Height;
            if (!double.IsNaN(this.Width))
            {
                if (this.Width < MyFormattedText.Width)
                    this.MinWidth = MyFormattedText.Width;
            }
            else
            {
                if (this.ActualWidth < MyFormattedText.Width)
                    this.MinWidth = MyFormattedText.Width;

            }
        }

    }
}

xaml界面代码:



    
        
    

 


 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955574.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号