0

I am having a class "BoolValue" where i declare a bool value and convert this into Dependency Property(Hope me had done that correct) Now in xaml where im having a checkbox wants to check/uncheck depending on bool value. Me attching the whole code guys, pls help.

<StackPanel Height="287" HorizontalAlignment="Left" Margin="78,65,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="309" DataContext="xyz" >
  <CheckBox Content="" Height="71" Name="checkBox1" IsChecked="{Binding Path=IsCkecked, Mode=TwoWay}"/>
</StackPanel>

And here is the class

public class BoolValue : INotifyPropertyChanged
    {        
        private bool _isCkecked;

        public bool IsCkecked
        {
            get { return _isCkecked; }
            set
            {
                if (value == _isCkecked)
                    return;

                _isCkecked = value;
                RaisePropertyChanged("IsCkecked");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string property)
        {
            PropertyChangedEventArgs args = new PropertyChangedEventArgs(property);
            var handler = this.PropertyChanged;
            //handler(this, args);
            if (handler != null)
            {
                handler(this, args);
            }
        }       
    }
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Dheeraj
  • 69
  • 1
  • 9

1 Answers1

0

What is the actual DataContext of your StackPanel? Looks like you're looking for property change but in different DataContext.

Providing BoolValue is your CheckBox's DataContext, below should work:

public class BoolValue : INotifyPropertyChanged
{ 
    private bool isChecked;
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    NotifyPropertyChanged("IsChecked");
                }
            }
        }


    public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(String propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
}

XAML:

<CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}"/>
StaWho
  • 2,488
  • 17
  • 24