The TextBox can't invoke PropertyChanged because of the focus on it. I implement it with pressing the Return
key after finishing the inputing. Below is my updated parts for your project:
Part 1: Add a MyCommand.cs
in your ViewModel
File.
class MyCommand : ICommand
{
private Func<object, bool> _canExecute;
private Action<object> _execute;
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null)
{
CommandManager.RequerySuggested += value;
}
}
remove
{
if (_canExecute != null)
{
CommandManager.RequerySuggested -= value;
}
}
}
public bool CanExecute(object parameter)
{
if (_canExecute == null) return true;
return _canExecute(parameter);
}
public void Execute(object parameter)
{
if (_execute != null && CanExecute(parameter))
{
_execute(parameter);
}
}
public MyCommand(Action<object> execute) : this(execute, null)
{
}
public MyCommand(Action<object> execute, Func<object, bool> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
}
Part 2: Replace your HoursLimitBox
TextBox with below code:
<TextBox x:Name="HoursLimitBox" Text="{Binding HoursLimitProp,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="36" Margin="133,10,0,0" VerticalAlignment="Top" Width="166" FontSize="20">
<TextBox.InputBindings>
<KeyBinding Command="{Binding EnterCommand}" Key="Return"></KeyBinding>
</TextBox.InputBindings>
</TextBox>
Part 3: Add below code in ViewModel.cs
private MyCommand _enterCommand;
public MyCommand EnterCommand
{
get
{
if (_enterCommand == null)
_enterCommand = new MyCommand(new Action<object>
(
o =>
{
HoursLimitConfProp = HoursLimitProp;
}
));
return _enterCommand;
}
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.