user3525444
user3525444

Reputation: 65

CheckBox binded property doesn`t change

I have the following code

Namespace WpfApplication1
{
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    using WpfApplication1.Annotations;
    using WpfApplication1.Enums;

    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private bool _isItemEnabled;

        public MainWindowViewModel()
        {
            this.IsItemEnabled = false;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public bool IsItemEnabled
        {
            get
            {
                return this._isItemEnabled;
            }

            set
            {
                this._isItemEnabled = value;
                this.OnPropertyChanged(nameof(this.IsItemEnabled));
            }
        }


        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



<CheckBox Grid.Row="0" Grid.Column="1" Margin="0,20" 
          IsChecked="{Binding Path = TimeIsEnabled, Mode=OneWay, UpdateSourceTrigger=PropertyChanged }"
          DataContext="{Binding ElementName = MainWindowViewModel}">
    TestIsEnabled
</CheckBox>

When i am clicking on the checkbox the property TimeIsEnabled located on code behind file doesn`t change and breakpoint on it doesn't fires too. I tried to locate this property at view model but the result was the same. Help please.

Upvotes: 0

Views: 119

Answers (1)

Slaven Tojić
Slaven Tojić

Reputation: 3014

Try to change Mode from OneWay to TwoWay (Mode=TwoWay). If you have OneWay binding the property updates the user interface, but the user interface doesn't update the property.

    <CheckBox Grid.Row="0" Grid.Column="1" Margin="0,20" 
              IsChecked="{Binding Path = TimeIsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }"
              DataContext="{Binding ElementName = MainWindowViewModel}">
        TestIsEnabled
    </CheckBox>

After analyzing the rest of the code you sent I've found some errors.First you are binding the wrong property TimeIsEnabled instead of IsItemEnabled.Second try to structure the DataContext like I did in the xaml file below:

<Window x:Class="WpfApplication3.MainWindow"
        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:local="clr-namespace:WpfApplication3"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <CheckBox Grid.Row="0" Grid.Column="1" Margin="0,20" 
          IsChecked="{Binding IsItemEnabled,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }">
            TestIsEnabled
        </CheckBox>
    </Grid>
</Window>

Your MainWindowViewModel class is fine. I tried this example and it works.

Upvotes: 2

Related Questions