WPF designer gives exception when databinding a label to a checkbox

Posted by John on Stack Overflow See other posts from Stack Overflow or by John
Published on 2010-04-01T22:32:01Z Indexed on 2010/04/01 22:33 UTC
Read the original article Hit count: 512

I'm sure it's something stupid, but I'm playing around with databinding. I have a checkbox and a label on a form. What I'm trying to do is simply bind the Content of the label to the checkbox's IsChecked value.

What I've done runs fine (no compilation errors and acts as expected), but if I touch the label in the XAML, the designer trows an exception:

System.NullReferenceException Object reference not set to an instance of an object. at MS.Internal.Designer.PropertyEditing.Editors.MarkupExtensionInlineEditorControl.BuildBindingString(Boolean modeSupported, PropertyEntry propertyEntry) at

<Window x:Class="UnitTestHelper.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:FileSysCtls="clr-namespace:WPFFileSystemUserControls;assembly=WPFFileSystemUserControls"
    xmlns:HelperClasses="clr-namespace:UnitTestHelper"
    Title="MainWindow" Height="406" Width="531">
<Window.Resources>
    <HelperClasses:ThreestateToBinary x:Key="CheckConverter" />
</Window.Resources>
<Grid Height="367" Width="509">
    <CheckBox Content="Step into subfolders" Height="16" HorizontalAlignment="Left" Margin="17,254,0,0" Name="chkSubfolders" VerticalAlignment="Top" Width="130" IsThreeState="False" />
    <Label Height="28" HorizontalAlignment="Left" Margin="376,254,0,0" Name="lblStepResult" VerticalAlignment="Top" Width="120" IsEnabled="True" Content="{Binding IsChecked, ElementName=chkSubfolders, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource CheckConverter}}" />
</Grid>

The ThreeStateToBinary class is as follows:

    class ThreestateToBinary : IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((bool)value)
            return "Checked";
        else
            return "Not checked";
        //throw new NotImplementedException();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((string)value == "Checked");
        //throw new NotImplementedException();
    }

    #endregion
}

Quite honestly, I'm playing around with it at this point. It was originally simpler (not using the ValueConverter) but was displaying similar behavior when I simply had the content set to:

Content="{Binding IsChecked, ElementName=chkSubfolders, UpdateSourceTrigger=PropertyChanged}" 

Any ideas?

Thanks,

John

© Stack Overflow or respective owner

Related posts about c#4.0

Related posts about databinding