为了扩展@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;