【发布时间】:2011-02-03 13:54:42
【问题描述】:
我确定这很愚蠢,但我正在玩数据绑定。我在表单上有一个复选框和一个标签。我要做的只是将标签的内容绑定到复选框的 IsChecked 值。
我所做的运行良好(没有编译错误并且按预期运行),但是如果我触摸 XAML 中的标签,设计器会抛出异常:
System.NullReferenceException 你调用的对象是空的。 在 MS.Internal.Designer.PropertyEditing.Editors.MarkupExtensionInlineEditorControl.BuildBindingString(布尔模式支持,PropertyEntry propertyEntry) 在
<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>
ThreeStateToBinary 类如下:
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
}
老实说,我现在正在玩弄它。它最初更简单(不使用 ValueConverter),但当我将内容设置为时显示类似的行为:
Content="{Binding IsChecked, ElementName=chkSubfolders, UpdateSourceTrigger=PropertyChanged}"
有什么想法吗?
谢谢,
约翰
【问题讨论】:
标签: data-binding c#-4.0 binding