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
{/// <summary>/// UserControl1.xaml 的交互逻辑/// </summary>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界面代码:
<UserControl x:Class="Test.UTextBlock"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:Test"mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"><Grid Name="grid1" Background="{Binding Background,RelativeSource={RelativeSource Mode= FindAncestor,AncestorType={x:Type local:UTextBlock}}}"></Grid>
</UserControl>
<userControl:UTextBlock Name="xx1" Text="1243" Margin="64,216,169,65" IsFenSanDuiQi="true" FontSize="32" BorderThickness="2" BorderBrush="Black"/>
<userControl:UTextBlock Name="xx1" Text="1243" Margin="64,216,169,65" IsFenSanDuiQi="False" FontSize="32" HorizontalContentAlignment="Right" BorderThickness="2" BorderBrush="Black"/>
WPF 分散对齐TextBlock相关推荐
- word分散对齐调整宽度_Word中文字很难对齐?有了这4个方法,2秒可对齐Word中所有文字...
在使用Word的时候,不少人有这样的烦恼:Word中的文字对齐不了,浪费了好多时间,还是弄不好.那么今天小编就来分享几个好用的Word中文字对齐的方法,下面就一起来看看吧~ 一.简单操作 1.分散对齐 ...
- WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法。...
WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法. 原文:WPF编程,TextBlock中的文字修饰线(上划线,中划线,基线与下划线)的使用方法. 版权声明:我不 ...
- WPF 绘制对齐像素的清晰显示的线条
WPF 绘制对齐像素的清晰显示的线条 原文:WPF 绘制对齐像素的清晰显示的线条 版权声明:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.欢迎转载.使用.重新发布, ...
- Word中两端对齐、分散对齐的区别
Word中两端对齐.分散对齐的区别 [转] http://www.dzwebs.net/3734.html 在Word文档中,默认的对齐方式为两端对齐,但是很多人都不知道两端对齐的效果到底是什么,因 ...
- 如何让Excel单元格中的名字分散对齐
1 操作方式 开始->对齐方式->对齐->水平对齐->分散对齐(缩进) 2 优势 不会破坏数据的有效性 转载于:https://www.cnblogs.com/pythonpr ...
- html表格字符分散,如何实现Word表格文字分散对齐?
回答: 解决表格文字分散对齐的问题 表格中的文字分散对齐也有它的不足之处,那就是它一般都是以单元格两端的表格线为标准,将所有文字平均分布于单元格中,这就存在一个文字距离表格线过近的问题. 该问题在单元 ...
- word分散对齐调整宽度_Word文档如何让不同字数对齐
Word文档每行字数对齐在合同上我们用的是最多的,比如这样的文字 有的两个字,有的四个字,不对齐的话看着实在是不好看,有的朋友手动敲空格强制对齐,我们今天来学习几种简单的方法. 一.文字对齐 1.分散 ...
- 两端分散对齐怎么设置_Word文档两端对齐.分散对齐如何设置
Word文档两端对齐.分散对齐如何设置 在Word文档排版中有许多细节和技巧都需要我们去掌握熟知,只有这样我们才可以在日常生活中快速的完成工作.今天小编要为大家介绍的是Word两端对齐与分散对齐功能. ...
- 怎么把图片分散对齐PHP,Word技巧系列封面如何快速对齐
没有对比就没有伤害! 你的Word封面是不是这样的: 大神的Word封面可能是这样的: 看完这两张图,还需要过多的解释吗? 为了达到图2这种效果,有的小伙伴会使用空格进行调整,但有时候即使使用空格,也 ...
最新文章
- PTA基础编程题目集-6-6 求单链表结点的阶乘和
- 使用R构建Xgboost模型并绘制ROC曲线
- 【OpenCV学习笔记之一】图像加载,修改及保存
- 在IIS6中FLV不能播放
- 克隆管理员帐号的方法
- 【MySQL通过视图(或临时表)实现动态SQL(游标】
- webpack debug
- JPA零碎要点---JTA全局事物理解
- contentsiz contentoffset contentInset的区别
- Solidity API
- 存储位置优化——把视图状态信息保存在服务端而非客户端
- mysql解压rpm文件在哪_[mysql] mysql 源码安装解压 rpm 包命令
- 一些软件工程的基础知识
- 如何在ps添加箭头_PS里怎么画箭头?PS画箭头三种方法介绍
- 【暴躁版】Java如何用IO流读取一个文本,按行输出到控制台?
- php格林威治时间,PHP默认时间是格林威治时间。
- 解决ubuntu 18.04安装搜狗输入法 在fcitx的add input method不显示
- 图像识别流程学习总结
- navigationController的一些用法
- BZOJ1616[Usaco2008 Mar]Cow Travelling游荡的奶牛 dp