textbox wpf 居中_WPF TextBlock中的文本垂直对齐方式

这篇博客介绍了一种方法,通过创建一个继承自Border的自定义控件CZ3r0_TextBox,来实现TextBox在WPF中的垂直居中对齐。该控件允许设置最大长度,并在焦点改变时调整边框颜色,同时设置了TextBox的光标为IBeam,以模拟文本输入效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了扩展@Orion Edwards提供的答案,这就是你如何完全从代码隐藏(没有样式集) . 基本上创建一个继承自Border的自定义类,将其Child设置为TextBox . 下面的示例假设您只需要一行,并且边框是Canvas的子项 . 还假设您需要根据Border的宽度调整TextBox的MaxLength属性 . 下面的示例还将边框的光标设置为模仿文本框,方法是将其设置为“IBeam”类型 . 设置边距“3”,以便TextBox不会完全对齐边框的左侧 .

double __dX = 20;

double __dY = 180;

double __dW = 500;

double __dH = 40;

int __iMaxLen = 100;

this.m_Z3r0_TextBox_Description = new CZ3r0_TextBox(__dX, __dY, __dW, __dH, __iMaxLen, TextAlignment.Left);

this.Children.Add(this.m_Z3r0_TextBox_Description);

类:

using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Shapes;

using System.Windows.Controls.Primitives;

namespace ifn0tz3r0Exp

{

class CZ3r0_TextBox : Border

{

private TextBox m_TextBox;

private SolidColorBrush m_Brush_Green = new SolidColorBrush(Colors.MediumSpringGreen);

private SolidColorBrush m_Brush_Black = new SolidColorBrush(Colors.Black);

private SolidColorBrush m_Brush_Transparent = new SolidColorBrush(Colors.Transparent);

public CZ3r0_TextBox(double _dX, double _dY, double _dW, double _dH, int _iMaxLen, TextAlignment _Align)

{

/

//TEXTBOX

this.m_TextBox = new TextBox();

this.m_TextBox.Text = "This is a vertically centered one-line textbox embedded in a border...";

Canvas.SetLeft(this, _dX);

Canvas.SetTop(this, _dY);

this.m_TextBox.FontFamily = new FontFamily("Consolas");

this.m_TextBox.FontSize = 11;

this.m_TextBox.Background = this.m_Brush_Black;

this.m_TextBox.Foreground = this.m_Brush_Green;

this.m_TextBox.BorderBrush = this.m_Brush_Transparent;

this.m_TextBox.BorderThickness = new Thickness(0.0);

this.m_TextBox.Width = _dW;

this.m_TextBox.MaxLength = _iMaxLen;

this.m_TextBox.TextAlignment = _Align;

this.m_TextBox.VerticalAlignment = System.Windows.VerticalAlignment.Center;

this.m_TextBox.FocusVisualStyle = null;

this.m_TextBox.Margin = new Thickness(3.0);

this.m_TextBox.CaretBrush = this.m_Brush_Green;

this.m_TextBox.SelectionBrush = this.m_Brush_Green;

this.m_TextBox.SelectionOpacity = 0.3;

this.m_TextBox.GotFocus += this.CZ3r0_TextBox_GotFocus;

this.m_TextBox.LostFocus += this.CZ3r0_TextBox_LostFocus;

/

//BORDER

this.BorderBrush = this.m_Brush_Transparent;

this.BorderThickness = new Thickness(1.0);

this.Background = this.m_Brush_Black;

this.Height = _dH;

this.Child = this.m_TextBox;

this.FocusVisualStyle = null;

this.MouseDown += this.CZ3r0_TextBox_MouseDown;

this.Cursor = Cursors.IBeam;

/

}

private void CZ3r0_TextBox_MouseDown(object _Sender, MouseEventArgs e)

{

this.m_TextBox.Focus();

}

private void CZ3r0_TextBox_GotFocus(object _Sender, RoutedEventArgs e)

{

this.BorderBrush = this.m_Brush_Green;

}

private void CZ3r0_TextBox_LostFocus(object _Sender, RoutedEventArgs e)

{

this.BorderBrush = this.m_Brush_Transparent;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值