当我尝试为我的XAML UserControl
重新加载设计器时,出现“未设置为对象实例的对象引用”错误。 Visual Studio中突出了以下行为的问题:WPF,设计器中的'未将对象引用设置为对象的实例'
<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"
Width="600" TemplateData="{Binding ElementName=cbo_templates,
Path=SelectedItem.Data, Mode=OneWay}"/>
TemplateDetail
是另一个UserControl
。当我查看TemplateDetail
时,它的Designer视图加载得很好,所以我认为那里没有问题。在我的名为cbo_templates
的XAML中有一个ComboBox
,其中包含我的Template
类的实例,该类具有Data
属性(因此SelectedItem.Data
)。但是,如果我从上述XAML中的Path
中删除.Data
,我仍然收到“对象引用”错误,所以我不认为问题在于我试图访问null
上的Path
属性。这里是我的ComboBox
XAML以防万一:
<ComboBox ItemsSource="{Binding Path=List}" Grid.Row="1" Grid.Column="3"
VerticalAlignment="Center" x:Name="cbo_templates" Width="250"
HorizontalAlignment="Left" DisplayMemberPath="Name"
SelectedValuePath="Name" SelectedIndex="0"/>
收到此错误是一个真正的问题,因为在设计视图将不加载,所以我不能看到我的UserControl
看起来不运行的应用程序。任何想法可能是错的?它构建得很好,我在Build Output中看不到任何绑定问题。
编辑:这里是两个UserControl
S中的构造函数代码:
构造的UserControl
与 “对象引用” 错误:
InitializeComponent();
grd_templateList.DataContext = this; // refers to containing <Grid> in XAML
的UserControl
构造我试图嵌入的其设计视图加载好的一个:
InitializeComponent();
grd_templateDetail.DataContext = this; // refers to containing <Grid> in XAML
编辑:我试图在设置DataContext
属性之前在构造函数中加入if (null != grd_templateList)
检查,但这没有帮助 - 重新加载Designer时仍然出现“对象引用”错误。
编辑:的ComboBox
使用List
属性是DependencyProperty
。我在Register
方法设置的默认值:
public static readonly DependencyProperty ListProperty =
DependencyProperty.Register(
"List",
typeof(List<Template>),
typeof(TemplateList),
new PropertyMetadata(
new List<Template> { _defaultTemplate }
)
);
即使我尝试初始化在构造函数中List
我UserControl
,我还是重装设计时得到的错误。我不认为问题是List
为空或SelectedItem.Data
是一个不好的路径。
编辑:没关系,哪怕只是有这导致我的设计不加载,给予“对象引用”错误:
<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"
TemplateData="{Binding}"/>
也有一些是它不喜欢有关TemplateData
财产的约束,显然。
编辑:添加到神秘的,我可以查看我的整体/主要Window
设计视图,其中包括UserControl
其设计图给我的“对象引用”错误。 O_o
在两个usercontrols的构造函数中是否有任何代码? – Goblin 2010-08-20 18:02:56
@Goblin:更新我的问题以显示构造函数代码。 – 2010-08-20 18:09:33