常用属性和设置1. VerticalContentAlignmentCenter 显示文字垂直居中HorizontalContentAlignmentCenter 显示文字水平居中实例1.数据绑定-简单对象的绑定2.输入数字 方法13.输入数字 方法2-----------------------------------------------------------------------------------------------------------------------实例1.数据绑定-简单对象的绑定绑定自定义的数据类对象在xaml代码中Binding标记扩展中仅定义了Path属性将它绑定到StudentData类的属性上。不需要定义源对象因为通过指定DataContext类定义源对象。DataContext是一个依赖属性它用基于FramewrokElement定义。指定相应控件的DataContext属性表示当前控件中的每个元素都默认绑定此数据。实例需要的文件新建类.cs数据模型类新建类后可以看到新建类BeanData.cs在主窗体类中应用2.输入数字TextBox x:Nametbx_coil_01 Width100InputMethod.IsInputMethodEnabledFalsePreviewKeyDowntbx_coil_01_PreviewKeyDown/TextBoxprivate void tbx_coil_01_PreviewKeyDown(object sender, KeyEventArgs e) { bool shiftKey (Keyboard.Modifiers ModifierKeys.Shift) ! 0;//判断shifu键是否按下 if (shiftKey true) //当按下shift { e.Handled true;//不可输入 } else //未按shift { if (!((e.Key Key.D0 e.Key Key.D9) || (e.Key Key.NumPad0 e.Key Key.NumPad9) || e.Key Key.Delete || e.Key Key.Back || e.Key Key.Tab || e.Key Key.Enter)) { e.Handled true;//不可输入 } }3.输入数字 方法2在WPFWindows Presentation Foundation中如果你想让一个 TextBox 控件只允许用户输入数字可以通过几种方法来实现。以下是几种常见的方法方法一使用 PreviewTextInput 事件你可以处理 TextBox 的 PreviewTextInput 事件并在事件处理程序中检查输入字符是否为数字。//xmlWindow x:ClassYourNamespace.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlTitleMainWindow Height200 Width400GridTextBox NameNumericTextBox Width200 Height30 Margin10PreviewTextInputNumericTextBox_PreviewTextInput//Grid/Window//csharp using System.Text.RegularExpressions; using System.Windows; using System.Windows.Input; namespace YourNamespace { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void NumericTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { e.Handled !IsTextAllowed(e.Text); } private static bool IsTextAllowed(string text) { // 允许数字0-9和小数点.如果不需要小数点可以去掉 . Regex regex new Regex(^[0-9](\\.[0-9]*)?$); return regex.IsMatch(text); } }将持续更新中