0

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.

1 Answers1

2

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.

Slaven Tojić
  • 2,945
  • 2
  • 14
  • 33
  • The problem was in datacontext – user3525444 Feb 04 '18 at 08:12
  • But i still getting error in ouput window System.Windows.Data Error: 40 : BindingExpression path error: 'TimeIsEnabled' property not found on 'object' ''String' (HashCode=2067111156)'. BindingExpression:Path=TimeIsEnabled; DataItem='String' (HashCode=2067111156); target element is 'CheckBox' (Name=''); target property is 'IsChecked' (type 'Nullable`1') – user3525444 Feb 04 '18 at 08:21
  • Have you implemented the INotifyPropertyChanged interface in your class where the TimeIsEnabled property is defined. Maybe this link will be helpful to your problem: https://stackoverflow.com/questions/5285377/wpf-mvvm-inotifypropertychanged-implementation-model-or-viewmodel – Slaven Tojić Feb 04 '18 at 11:51
  • Regarding your error maybe you can find a solution at this link : https://stackoverflow.com/questions/33805067/how-to-resolve-a-binding-expression-path-error-in-wpf – Slaven Tojić Feb 04 '18 at 12:12
  • The fact is this is the test application with one xaml file and view model that contains one property – user3525444 Feb 04 '18 at 23:13
  • I have modified the your code and it works for me. I hope this will help you. – Slaven Tojić Feb 05 '18 at 09:08