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 Val开发者_开发问答ueConverter) but was displaying similar behavior when I simply had the content set to:
Content="{Binding IsChecked, ElementName=chkSubfolders, UpdateSourceTrigger=PropertyChanged}"
Any ideas?
Thanks,
John
Try removing UpdateSourceTrigger=PropertyChanged
. In this case, the checkbox is your source and the label is your target. The label doesn't change, and furthermore, you set the mode to OneWay
which binds only from the source to the target. Therefore, it's meaningless to tell it the binding to update the source on property changed. It might not be causing your problem, but it seems suspect (or at least weird).
精彩评论