WPF简单的数据绑定示例

    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>

``
posted @   长安626  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示