Wpf – Bound Property always returns false but WPF bound CheckBox is still checked

checkboxdata-bindingwpf

I have a checkbox in a UserControl:

<CheckBox Content="Existing" IsChecked="{Binding Path=IsExistingTemplate, Mode=TwoWay}"/>

It is bound to a DataContext with Property IsExistingTemplate which always returns False. (In my real application, it doesn't always return False!). DataContext implements INotifyPropertyChanged.

public bool? IsExistingTemplate
{
get
{
return false;
}
set
{
OnPropertyChanged("IsExistingTemplate")
}
}

When the user clicks the CheckBox, the CheckBox always shows a tick.

How can I force the CheckBox not to show a tick when the user Clicks it?

Best Answer

I remember having a very similar problem, but I can't find where and how I solved it. I thought that the problem was caused by how WPF updates bindings, and I thought that I read somewhere that it could be solved by using an IValueConverter that simply passes the values through, but I've tested that it and it doesn't seem to work. It was supposed to work because using a converter was supposed to make the bindings re-evaluate.

You could always handle the Clicked event on the CheckBox and update the values via code, but it just feels so dirty. Sorry I couldn't be of more help

<StackPanel.Resources>
    <local:PassThroughConverter x:Key="PassThroughConverter" />
</StackPanel.Resources>
<CheckBox IsChecked="{Binding IsExistingTemplate, Converter={StaticResource PassThroughConverter}}" Content="Stuff"/>

public class PassThroughConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}
Related Topic