1

What i am trying to achieve is, when the check box is checked, the textbox will display True, when the checkbox is unchecked, text box will display False.

The issue at the moment is that initially it works, the box will be checked, and textbox will display true. But when i uncheck it, the text box remains saying True.

Sorry if i am missing something simple, still trying to grasp Wpf and c#.

Thanks for helping.

Xaml:

<Grid>
<CheckBox Content="Check me" HorizontalAlignment="Left" Height="19" Margin="88,63,0,0" VerticalAlignment="Top" Width="86"  IsChecked="{Binding CurrentValue, Mode= TwoWay}"/>
<TextBlock Text="{Binding CurrentValue, Mode=TwoWay}" HorizontalAlignment="Left" Height="39" Margin="88,82,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="86"/>
</Grid>

MainWindow:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainView();
    }

}

MainView:

 public class MainView : INotifyPropertyChanged
    {  

    private bool currentValue = true;
    public event PropertyChangedEventHandler PropertyChanged;

    public bool CurrentValue
    {
        get
        {
            return currentValue;
        }
        set
        {
            if (currentValue == value)
            {
                return;
            }
                currentValue = value;
                RaisePropertyChange("Check_Value");

        }
    }



    protected void RaisePropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }

}

2 Answers 2

2

You are setting your property wrongly following how you can fix this correctly

  public bool CurrentValue
    {
        get
        {
            return currentValue;
        }
        set
        {
            if (currentValue != value)
            {    
              currentValue = value;
             RaisePropertyChange("CurrentValue");
            }



    }
}
0
1

Instead of

RaisePropertyChange("Check_Value");

try

RaisePropertyChange("CurrentValue");

Value that pass must be the same as your property name

1
  • when you raise INotifyPropertyChanged.PropertyChanged event property name that you pass must be exactly the same as the one in your view model that you want to be refreshed. It's also case sensitive so CurrentValue is not the same as for example currentvalue
    – dkozl
    Feb 19, 2014 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.