使用 Prism 绑定 WpfToolKit 水印文本框时出现问题

Trouble Binding WpfToolKit Watermark Textbox Using Prism

所以,我在这上面花了太多时间试图找出我做错了什么。我也是 Prism 的新手。我看过其他人的绑定示例,但其中 none 似乎有效。对我来说没有做的是我将它绑定在 "Text=" 内并去点击我的文本清除的另一个文本框。我试过 TwoWay Mode、UpdatePropertyChange 等。似乎没有任何效果。我什至设置了断点以查看该值是否确实与 cs 代码匹配。但是不,它没有约束力。我究竟做错了什么?我只希望我的文本框等于我的 viewmodels "UserName" 字符串值,这样当有人点击登录按钮时,它会检查这些值以查看它们是否存在于数据库中。

这里是Login.xaml:

        <Grid x:Name="UserInputGrid"
          Margin="15" 
          Height="100">
        <wpfTool:WatermarkTextBox  x:Name="UsernameTextBox" 
                                   Width="600"
                                   Height="100"
                                   Text="{Binding UserName}"
                                   Watermark="Username"
                                   Cursor="IBeam" 
                                   FontFamily="Quicksand" 
                                   FontSize="72"/>
    </Grid>

LoginView.xaml.cs

    public LoginView()
    {
        InitializeComponent();
        DataContext = new LoginViewModel();
    }
}

LoginViewModel.cs:

public class LoginViewModel : BindableBase
{
    #region Private Variables

    //Private Variables

    bool isCanLogin = true;
    private string _username;
    private string _password;
    #endregion

    #region Public Variables

    //Public Variables

    public string UserName
    {
        get { return _username; }
        set { RaisePropertyChanged("UserName"); }
    }
    public string Password
    {
        get { return _password; }
        set { RaisePropertyChanged("Password"); }
    }
    public bool CanLogin() { return isCanLogin; }

    #endregion

    #region Commands

    //Commands

    RelayCommand _loginCommand;
    public ICommand LoginCommand
    {
        get
        {
            if (_loginCommand == null)
                _loginCommand = new RelayCommand(Login, CanLogin);

            return _loginCommand;
        }
    }
    #endregion

    #region Functions and Methods

    //Functions and Methods

    private void Login()
    {
        try
        {
            isCanLogin = false;
            SoundPlayers.ButtonSound();
            //Authentication and Checks the DB to see if the User exists

            if (AuthenticationService.VerifyUser(UserName, Password))
            {
                //Navigate to landing page
                SoundPlayers.LoginSound();
            }
            else
            {
                //Doesn't exist!?!
            }

            isCanLogin = true;
        }
        catch
        {
            //If Login requirements aren't met, or failed
        }
        finally
        {
            isCanLogin = true;
        }
    }
    #endregion
}

你的二传手坏了。他们从不更新私有支持字段。控件通过绑定正确更新您的 属性,但 属性 不会保存进来的值。您需要以类似方式更新 Password

public string UserName
{
    get { return _username; }
    set {
        if (_username != value)
        {
            _username = value;
            RaisePropertyChanged("UserName");
        }
    }
}

将您的 属性 setter 更改为如下内容:

public string UserName
{
    get { return _username; }
    set { SetProperty(ref _username, value); }
}

使用此技术,您不必调用 RaisePropertyChanged,它已为您完成。并且,如果支持变量已更改,则会设置它。

SetProperty 方法来自 Prism.Mvvm 命名空间。