Skip to main content
 首页 » 编程设计

c#之WPF数据绑定(bind)无法获取数据

2024年02月13日15softidea

我正在使用 WPF 和 C# 编写一个登录窗口,但是当我试图从 TextBox 中检索用户名时卡住了。类我绑定(bind)到 TextBox 的属性总是得到一个空值,我不知道为什么。

MainWindow.xaml

<Window x:Class="Databinding.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="256*"/> 
        <ColumnDefinition Width="261*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
        <RowDefinition Height="*"/> 
        <RowDefinition Height="*"/> 
        <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Label Grid.Row="0" Grid.Column="0" Content="Username" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="20"/> 
    <Label Grid.Row="1" Grid.Column="0" Content="Password" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" FontSize="20"/> 
    <Button Content="Confirm" Click="Confirm_Click" IsDefault="True" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" FontSize="20"/> 
    <Button Content="Cancel" Click="Cancel_Click" IsCancel="True" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" FontSize="20"/> 
    <TextBox x:Name="TextUsername" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0" TextWrapping="Wrap"  VerticalAlignment="Stretch" ToolTip="Enter your username"/> 
    <PasswordBox x:Name="Password" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,0,0,0"  VerticalAlignment="Stretch" ToolTip="Enter your password"/> 
 
</Grid> 

MainWindow.xaml.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.ComponentModel; 
 
namespace Databinding 
{ 
    /// <summary> 
    /// MainWindow.xaml 的交互逻辑 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
        public MainWindow() 
        { 
            InitializeComponent(); 
        } 
 
        private void Confirm_Click(object sender, RoutedEventArgs e) 
        { 
            Login LoginInfo = new Login(); 
 
            Binding bindingLogin = new Binding(); 
            bindingLogin.Source = LoginInfo; 
            bindingLogin.Path = new PropertyPath("Username"); 
            bindingLogin.Mode = BindingMode.TwoWay; 
            bindingLogin.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
            BindingOperations.SetBinding(this.TextUsername, TextBox.TextProperty,    bindingLogin); 
 
        if(LoginInfo.Username=="admin" && this.Password.Password=="admin") 
        { 
            MessageBox.Show("Welcome!","Login Status"); 
        } 
        else 
        { 
            MessageBox.Show("Something is wrong!","Login Status"); 
        } 
    } 
 
    private void Cancel_Click(object sender, RoutedEventArgs e) 
    { 
        this.Close(); 
    } 
} 
public class Login:INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    private string username; 
    public string Username 
    { 
        get 
        { 
            return username; 
        } 
        set 
        { 
            username = value; 
            if(this.PropertyChanged!=null) 
            { 
                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Username")); 
                } 
            } 
        } 
    } 
} 

我知道在这种情况下数据绑定(bind)不是一个明智的选择,我可以通过使用来更有效地完成事情

string Username = this.TextUsername.Text 

无论如何,这是一个演示,我必须在我的项目中使用数据绑定(bind)。我的代码有什么问题吗?

请您参考如下方法:

在您附加绑定(bind)时,Login.Username 为 null,并且由于绑定(bind)是两种方式,WPF 会将您的文本框更新为 null 以匹配。

绑定(bind)应该始终处于事件状态,并在 XAML 中声明,而不是绑定(bind)在您需要数据的位置。您正在破坏使用绑定(bind)的目标。就像您说的那样,如果您要明确地进行操作,直接抓取文本会容易得多。