[英]Disable button when 1 or more TextBox fields are empty
I tried a few solution but they seems to be for WPF.我尝试了一些解决方案,但它们似乎适用于 WPF。
This is what I have for the moment but it doesn't seems to work:这是我目前所拥有的,但它似乎不起作用:
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Username :"/>
<TextBox x:Name="userNameTextBox" Text="{Binding Username, Mode=TwoWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Password :" />
<TextBox x:Name="passwordTextBox" Text="{Binding Password, Mode=TwoWay}" />
</StackPanel>
<Button IsEnabled="{Binding ElementName=userNameTextBox, Path=Text.Length}" Command="{Binding LoginCommand }" Content="Login" />
</StackPanel>
</Grid>
So I want to make the button enables if userNameTextBox and passwordTextBox are not empty.因此,如果 userNameTextBox 和 passwordTextBox 不为空,我想让按钮启用。
It stays enabled all the time.它始终保持启用状态。 What am I doing wrong?我究竟做错了什么? Is it possible to do this only via XAML?是否可以仅通过 XAML 执行此操作?
private string _username;
public string Username
{
get { return _username; }
set
{
_username = value;
RaisePropertyChanged(nameof(Username));
RaisePropertyChanged(nameof(IsButtonEnable));
}
}
private string _password;
public string Password
{
get { return _password; }
set {
_password = value;
RaisePropertyChanged(nameof(Password));
RaisePropertyChanged(nameof(IsButtonEnable));
}
}
private bool _isButtonEnable;
public bool IsButtonEnable
{
get
{
if (!string.IsNullOrEmpty(_username) && !string.IsNullOrWhiteSpace(_username)
&& !string.IsNullOrEmpty(_password) && !string.IsNullOrWhiteSpace(_password))
{
return true;
}
else
{
return false;
}
}
}
I think you can set a boolean value to disable that button.我认为您可以设置 boolean 值来禁用该按钮。 Here is sample code given below=>这是下面给出的示例代码=>
XAML: XAML:
<Grid>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Username :"/>
<TextBox x:Name="userNameTextBox" Text="{Binding Username, Mode=TwoWay}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Password :" />
<TextBox x:Name="passwordTextBox" Text="{Binding Password, Mode=TwoWay}" />
</StackPanel>
<Button IsEnabled="{Binding IsButtonEnable}" Command="{Binding LoginCommand }" Content="Login" />
</StackPanel>
</Grid>
In ViewModel Add IsButtonEnable Property Like在ViewModel中添加 IsButtonEnable 属性,如
#region Property IsButtonEnable: bool
public bool IsButtonEnable
{
get
{
if( !string.IsNullOrEmpty(_Username) && !string.IsNullOrWhiteSpace(_Username)
&& !string.IsNullOrEmpty(_Password) && !string.IsNullOrWhiteSpace(_Password))
{
return true;
}
else
{
return false;
}
}
}
#endregion
#region Property Username: string
private string _Username;
public string Username
{
get { return _Username; }
set
{
_Username = value;
NotifyPropertyChanged(nameof(Username));
NotifyPropertyChanged(nameof(IsButtonEnable));
}
}
#endregion
#region Property Password: string
private string _Password;
public string Password
{
get { return _Password; }
set
{
_Password = value;
NotifyPropertyChanged(nameof(Password));
NotifyPropertyChanged(nameof(IsButtonEnable));
}
}
#endregion
Note: Here NotifyPropertyChanged
will call IsButtonEnable
to update UI when either Username
or Password
is changed.注意:此处NotifyPropertyChanged
将在Username
名或Password
更改时调用IsButtonEnable
来更新 UI。 You want to add more property then add them and update if condition inside IsButtonEnable
get
section.您想添加更多属性,然后添加它们并更新IsButtonEnable
get
部分中的条件。
Please check and let me know.请检查并让我知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.