public partial class index : INotifyPropertyChanged
{
public index()
{
InitializeComponent();
DataContext = this;
}
private string _userName;
public string UserName
{
get => _userName;
set
{
if (_userName != value)
{
_userName = value;
OnPropertyChanged(nameof(UserName));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void ValidateUserName()
{
// 可以在这里添加验证逻辑
if (string.IsNullOrEmpty(UserName))
{
Growl.Error("值不能为空!请重新输入!");
}
else
{
Growl.Success("用户输入的值为:"+UserName);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ValidateUserName();
}
}
}
ui:
<Window
x:Class="WpfApp1.index"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:local="clr-namespace:WpfApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="index"
Width="800"
Height="450"
mc:Ignorable="d">
<Grid>
<StackPanel Width="200">
<TextBox
Margin="0,0,0,10"
hc:InfoElement.Necessary="True"
hc:InfoElement.Placeholder="请输入用户名"
hc:InfoElement.Title="用户"
hc:InfoElement.TitlePlacement="Left"
Style="{StaticResource TextBoxExtend}"
Text="{Binding UserName, Mode=TwoWay}" />
<!-- 密码 -->
<!--
<PasswordBox
x:Name="txtPassword"
Margin="0,0,0,20"
hc:InfoElement.Necessary="True"
hc:InfoElement.Placeholder="请输入密码"
hc:InfoElement.ShowClearButton="True"
hc:InfoElement.Title="密码"
hc:InfoElement.TitlePlacement="Left"
Style="{StaticResource PasswordBoxExtend}" />-->
<!-- 登录按钮 -->
<Button
Width="150"
Height="30"
Margin="20,0,0,0"
Padding="0"
Click="Button_Click"
Content="登录"
FontFamily="/WpfApp1;component/Themes/font/#iconfont"
FontSize="20"
Style="{StaticResource ButtonSuccess}" />
</StackPanel>
</Grid>
</Window>
``
标签:UserName,userName,string,示例,void,绑定,private,WPF,public
From: https://www.cnblogs.com/gyl0812/p/18556354