当前位置:网站首页>WPF中报错:“未将对象引用设置到对象的实例。”
WPF中报错:“未将对象引用设置到对象的实例。”
2022-07-31 12:07:00 【Monkey_King_GL】
项目场景:
在WPF中,我们经常需要使用到各种控件的属性,不管是在后端还是前端,都避免不了对控件的操作,但是大家在使用的过程中我们一定要注意一个很重要的细节!!!
问题描述
今天我想从后台动态设置前端控件的属性时,突然遇到一个问题,我的控件都显示“未将对象引用设置到对象实例”。代码如下:
前端代码
<Grid Grid.Column="2">
<TextBlock HorizontalAlignment="Left"
Margin="0 6 0 6"
TextWrapping="Wrap"
Text="断面类型:"
VerticalAlignment="Center"
Height="15"
Width="60" />
</Grid>
后端代码:
public void CrossSectionTextIsEnabled(BIM_CrossSection crossSection, bool temp)
{
//this.SectionName.IsEnabled = temp;
//this.SectionType.Text = crossSection.EarlySupportFill.ToString();
this.SectionType.IsEnabled = temp;
//this.SectionLevel.IsEnabled = temp;
//this.IsArchUp.IsEnabled = temp;
//this.ArchUpFill.IsEnabled = temp;
this.SecondArch.IsEnabled = temp;
this.SecondUp.IsEnabled = temp;
this.FirstArch.IsEnabled = temp;
this.FirstUp.IsEnabled = temp;
this.R4.IsEnabled = temp;
}
这段代码表面上看起来确实没什么问题,也符合WPF的语法。
原因分析:
后来通过认真查看代码,发现在初始化组件的时候出问题了。
错误示范:
public Win_ManageWindow(string sectionId)
{
Id = sectionId;
CrossSectionTextIsEnabled(crossSection, temp);
InitializeComponent();
LoadSource(sectionId, 1);
}
InitializeComponent()函数是初始化前端组件的,是将前端的各个组件加载到后端的。因此该代码的调用应该位于调用组件之前!!!
解决方案:
将InitializeComponent()函数设置到调用组件之前,即可解决该问题。
public Win_ManageWindow(string sectionId)
{
Id = sectionId;
InitializeComponent();
CrossSectionTextIsEnabled(crossSection, temp);
LoadSource(sectionId, 1);
}
边栏推荐
猜你喜欢
Docker practical experience: Deploy mysql8 master-slave replication on Docker
学习爬虫之Scrapy框架学习(1)---Scrapy框架初学习及豆瓣top250电影信息获取的实战!
多线程学习笔记-2.final关键字和不变性
Initial JDBC programming
Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?
St. Regis Takeaway Project: New dishes and dishes paged query
订song餐系统
vb.net 画曲线
随机推荐
关于==和equals的区别和联系,面试这么回答就可以
VBA输出日志到工作簿demo
如何正确地把服务器端返回的文件二进制流写入到本地保存成文件
JVS低代码能力简介及功能清单
R 语言data.frame 中的另一行中减去一行
ESP8266-Arduino编程实例-PIR(被动红外)传感器驱动
St. Regis Takeaway Project: File Upload and Download
dosbox基础使用[通俗易懂]
[Virtualization Ecological Platform] Platform Architecture Diagram & Ideas and Implementation Details
502 bad gateway causes and solutions
DCM 中间件家族迎来新成员
CameraToolUnity中两种摄像机的两种观察控制方式
file contains vulnerabilities
带有对称约束切换线性系统的结构可控性
Life is endless, there are more questions, simple questions to learn knowledge points
一文吃透接口调用神器RestTemplate
关于IDEA开发工具的介绍
MySQL日志中“binlog”的三种格式玩起来真爽
DCM middleware family welcomes a new member
生信周刊第38期