Text Block Bined to a Property in the View Model, and Implementing the INotifyPropertyChanged.
The Text does not update, even though the property value has changed. once I try to edit anything in the XAML of the TextBlock, the text appears.
I know that this may not be logical.
Here is the ViewModel Base Class :
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnpropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Here is the View Model Binded to the WPF :
public class LoginViewModel : ViewModelBase
{
private string _errormessage = "";
public string ErrorMessage
{
get
{
return _errormessage;
}
set
{
_errormessage = value;
OnpropertyChanged(nameof(_errormessage));
}
}
public LoginViewModel()
{
LoginCommand = new ViewModelCommand(ExecuteLoginCommand, CanExecuteLoginCommand);
}
private void ExecuteLoginCommand(object obj)
{
try
{
///Some Code that can return True or False
///When the result is false the Error Message Property Should Display some text
bool valid = false;
if (valid)
{
ErrorMessage = "";
}
else if (!valid)
{
>>>>>>>>> ErrorMessage = "Username or Password is Wrong";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
ErrorMessage = "Couldn't Connect to Server";
}
}
As You can See in the code above, ErrorMessage
takes a new value if the code is true or false.
Here Comes the Issue :
in the UI WPF View, the Text Does not appear.
Here is the XAML of the TextBlock
<TextBlock x:Name="txtError"
Text="{Binding ErrorMessage, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
FontSize="15"
FontWeight="Normal"
Margin="0,10,0,0"
Foreground="Red"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="Visible"/>
If I tried to change for Example the UpdateTriggerSource
during debugging, suddenly the Text will appear.
can anyone locate the issue?