Я создал контекст, в котором есть два поля для текстбоксов и поле статуса формы (PasswordBox пока намеренно не использую, т.к. с ним отдельные проблемы):
namespace GuiBuyerDesktop.Windows.LoginWindow {
internal class LoginWindowContext : BaseViewModel {
private string _Login;
private string _Password;
public string Login
{
get => _Login;
set => Set(ref value, _Login);
}
public string Password
{
get => _Password;
set => Set(ref value, _Password);
}
public bool IsFormReadyToSend
{
get {
return (_Login is not null && _Password is not null);
}
}
}
}
BaseViewModel:
namespace WPFCore {
public abstract class BaseViewModel : INotifyPropertyChanged {
// All bindings subscribed to this event
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChaged([CallerMemberName] string PropertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
protected virtual bool Set<T>(ref T field, T value, [CallerMemberName] string PropertyName = null)
{
if (Equals(field, value)) return false;
field = value;
OnPropertyChaged(PropertyName);
return true;
}
}
}
XAML:
<Window x:Class="GuiBuyerDesktop.Windows.LoginWindow.LoginWindowView"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:GuiBuyerDesktop.Windows.LoginWindow"
mc:Ignorable="d"
Title="Вход"
Width="350"
SizeToContent="Height"
ResizeMode="CanMinimize">
<Window.DataContext>
<vm:LoginWindowContext/>
</Window.DataContext>
<StackPanel>
<TextBlock
Background="#fcec5e"
Height="65"
FontFamily="{StaticResource AR}"
FontSize="45"
TextAlignment="Center"
Padding="0,2,0,0">
Главпоставка
</TextBlock>
<StackPanel Margin="30,10,30,25">
<StackPanel.Resources>
<Style x:Key="BaseInputStyle" TargetType="{x:Type Control}">
<Setter Property="Margin" Value="0,15,0,0"/>
<Setter Property="Height" Value="40"/>
<Setter Property="BorderBrush" Value="#d4b2ff"/>
<Setter Property="BorderThickness" Value="3"/>
<Setter Property="FontSize" Value="17"/>
<Setter Property="Padding" Value="4,4,0,0"/>
</Style>
</StackPanel.Resources>
<TextBox
Style="{StaticResource BaseInputStyle}"
Text="{Binding Path=Login, UpdateSourceTrigger=LostFocus, Mode=OneWayToSource}"
>
</TextBox>
<TextBox
Style="{StaticResource BaseInputStyle}"
Text="{Binding Path=Password, UpdateSourceTrigger=LostFocus, Mode=OneWayToSource}"
>
</TextBox>
<!--<PasswordBox
Style="{StaticResource BaseInputStyle}"
Password="{Binding Path=Password, UpdateSourceTrigger=PropertyChanged}"
></PasswordBox>-->
<Button
Background="#fcec5e"
BorderBrush="Black"
BorderThickness="2"
Height="45"
Margin="0,15,0,0"
FontFamily="{StaticResource AR}"
FontSize="22"
IsEnabled="{Binding Path=IsFormReadyToSend}"
>
Вход
</Button>
</StackPanel>
</StackPanel>
</Window>
Почему кнопка остается не активной, когда оба поля заполнены?