user5856424
user5856424

Reputation:

Bool Property won't update via Checkbox Binding

In my WPF MVVM Project (with MVVM Light) I binded the CheckBox IsChecked Property to a bool Property IsTermsOfUseAccepted from a ValidationUserViewModel Property in my LoginRegisterViewModel likes this

public interface IValidateUserViewModel
{
    // Other Properties..

    bool IsTermsOfUseAccepted { get; set; }

    bool IsRegistrationValid { get; }
}

public class ValidateUserViewModel : ViewModelBase, IDataErrorInfo, IValidateUserViewModel
{
    public bool IsTermsOfUseAccepted
    {
        get { return _isTermsOfUseAccepted; }
        set { Set(ref _isTermsOfUseAccepted, value); }
    }

    public bool IsRegistrationValid
        // IDataErrorInfo Stuff
        => ValidatePropertiesRegistration.All(property => GetValidationError(property) == null) 
           && IsTermsOfUseAccepted;
}

// ------------------------------------------------------------------------------------

public class LoginRegisterViewModel : ViewModelBase
{
    public LoginRegisterViewModel(
        IValidateUserViewModel validateUserViewModel,
        IUserRepository userRepository)
    {
        _userRepository = userRepository;

        ValidateUserViewModel = validateUserViewModel;

        RegisterCommand = new RelayCommand(RegisterExecute, RegisterCanExecute);
    }

    public IValidateUserViewModel ValidateUserViewModel
    {
        get { return _validateUserViewModel; }
        set { Set(ref _validateUserViewModel, value); }
    }

    // RegisterCommand Stuff

    public bool RegisterCanExecute() => ValidateUserViewModel.IsRegistrationValid;
}

XAML

<UserControl x:Class="Messenger4u.View.LoginRegisterView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:control="clr-namespace:Messenger4u.View.Control"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
         DataContext="{Binding LoginRegister,
                               Source={StaticResource Locator}}"
         d:DesignHeight="300"
         d:DesignWidth="300"
         mc:Ignorable="d">

    <!-- Stuff -->

    <CheckBox IsChecked="{Binding ValidateUserViewModel.IsTermsOfUseAccepted}" />

    <!-- Stuff -->

</UserControl>

But my problem is, that all Properties binded to TextBoxes etc. just working finde except the IsTermsOfUseAccept Property. I already tried to set manually RaisePropertyChanged, putting the bool in the LoginRegisterViewModel directly, setting in XAML UpdateSourceTrigger=PropertyChanged and Mode=TwoWay, but nothing worked.

What could be the problem?

Upvotes: 2

Views: 758

Answers (1)

NoName
NoName

Reputation: 8025

Change IsChecked binding to TwoWay and set UpdateSourceTrigger to be explicit:

<CheckBox IsChecked="{Binding ValidateUserViewModel.IsTermsOfUseAccepted, Mode=TwoWay, UpdateSourceTrigger=Explicit}" />

And change this:

public bool IsTermsOfUseAccepted

To:

bool? _isTermsOfUseAccepted = false; // or true, if you like
public bool? IsTermsOfUseAccepted

add the ? mark

Upvotes: 3

Related Questions