WPF,在设计师的对象引用不设置到对象的实例“ [英] WPF, 'Object reference not set to an instance of an object' in Designer
问题描述
我发现了一个对象未设置为一个对象的实例错误,当我尝试重新加载设计我的XAML 用户控件
。 Visual Studio中突出了以下行作为问题:
I'm getting an "Object reference not set to an instance of an object" error when I try to reload the Designer for my XAML UserControl
. Visual Studio highlights the following line as being the problem:
<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"
Width="600" TemplateData="{Binding ElementName=cbo_templates,
Path=SelectedItem.Data, Mode=OneWay}"/>
TemplateDetail
是另一个用户控件
。当我查看 TemplateDetail
,它的设计师认为负荷就好了,所以我不认为有一个问题。有一个组合框
在我的XAML命名为 cbo_templates
包含我的模板的实例
类,它有一个数据
属性(因此 SelectedItem.Data code>)。但是,如果我删除
。数据
从上面的XAML的路径
,我仍然得到对象的错误,所以我不认为这个问题是,我试图访问路径
财产空
。这是我的组合框
XAML以防万一:
TemplateDetail
is another UserControl
. When I view TemplateDetail
, its Designer view loads just fine, so I don't think there's a problem there. There is a ComboBox
in my XAML named cbo_templates
that contains instances of my Template
class, which has a Data
property (hence SelectedItem.Data
). However, if I remove .Data
from the Path
in the above XAML, I still get the "Object reference" error, so I don't think the problem is that I'm trying to access the Path
property on null
. Here's my ComboBox
XAML just in case:
<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"/>
收到此错误是一个真正的问题,因为在设计视图将不加载,所以我不能看到我的用户控件
貌似不运行应用程序。任何想法可能是错误的?它建立不错,我没有看到生成输出任何有约束力的问题。
Getting this error is a real problem because the Design view won't load, so I can't see what my UserControl
looks like without running the app. Any idea what could be wrong? It builds fine and I don't see any binding problems in the Build Output.
编辑:这里的构造函数code为用户控件
S:
here is the constructor code for both UserControl
s:
的用户控件
构造与对象的错误:
InitializeComponent();
grd_templateList.DataContext = this; // refers to containing <Grid> in XAML
的用户控件
我试图嵌入,指其设计视图负荷好吗构造器:
Constructor of UserControl
I'm trying to embed, the one whose Design view loads okay:
InitializeComponent();
grd_templateDetail.DataContext = this; // refers to containing <Grid> in XAML
编辑:我试图把一个如果(!空= grd_templateList)
检查设置之前,建设者的的DataContext
属性,但是这并没有帮助 - 仍然得到对象的错误重新加载设计时
I tried putting an if (null != grd_templateList)
check in the constructors before setting their DataContext
properties, but that didn't help--still getting the "Object reference" error when reloading the Designer.
编辑:的列表
的组合框
使用属性是的DependencyProperty
。我在注册
方法,设置默认值:
the List
property that the ComboBox
uses is a DependencyProperty
. I have a default value set in the Register
method:
public static readonly DependencyProperty ListProperty =
DependencyProperty.Register(
"List",
typeof(List<Template>),
typeof(TemplateList),
new PropertyMetadata(
new List<Template> { _defaultTemplate }
)
);
即使我尝试初始化列表
在构造函数中为我的用户控件
,我还是重装时出现错误该设计器。我不认为这个问题是列表
为空或 SelectedItem.Data code>是一个坏的路径。
Even if I try to initialize List
in the constructor for my UserControl
, I still get the error when reloading the Designer. I don't think the problem is that List
is null or SelectedItem.Data
is a bad path.
编辑:没关系,哪怕只是有这个原因我的设计不加载,给对象的错误:
okay, even just having this causes my Designer to not load, giving the "Object reference" error:
<local:TemplateDetail Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3"
TemplateData="{Binding}"/>
也有一些是它不喜欢有关 TemplateData code>属性绑定,显然。
编辑:添加到神秘,我可以查看我的整体/主窗口
,其中包括<$ C设计视图$ C>用户控件的设计视图给我的对象的错误。 O_O
to add to the mystery, I can view the Design view of my overall/main Window
, which includes the UserControl
whose Design view gives me the "Object reference" error. O_o
推荐答案
什么亚历克斯说是要走的路。但我认为它有点混乱明白他在说什么。
What Alex says is the way to go. But I think its a little confusing to understand what he is saying.
假设你在Visual Studio中打开项目,打开另一个Visual Studio的实例,然后选择的调试 - >附加到进程的。在其中打开选择对话框
Assuming you have your project open in Visual Studio, open another Visual Studio instance and select Debug->Attach To Process. In the dialog which opens select
-
XDesProc.exe
(这是XAML UI设计器)对于<strong> VS2012和更新或 -
devenv.exe的
为年长VS 版本。
XDesProc.exe
(which is the XAML UI Designer) for VS2012 and newer ordevenv.exe
for older VS versions.
然后做刷新设计,为用户控制和看到第二VS实例的输出来检查到底是什么错误。
Then do "Reload Designer" for the user control and see the output in the second VS instance to check what exactly is the error.
这篇关于WPF,在设计师的对象引用不设置到对象的实例“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!