当前位置:网站首页>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);
}
边栏推荐
- Standard SQL/JSON - the sobering part
- Docker build Mysql master-slave replication
- Quickly learn database management
- 科学论文和学术论文写作
- In Excel using ODBC consumer SAP ABAP CDS view
- [Virtualization ecological platform] Raspberry Pi installation virtualization platform operation process
- 【核心概念】图像分类和目标检测中的正负样本划分以及架构理解
- 三相PWM整流器预测直接功率控制
- JVM 运行时数据区与JMM 内存模型详解
- dosbox基础使用[通俗易懂]
猜你喜欢

mysql根据多字段分组——group by带两个或多个参数

Initial JDBC programming

线性表的基本概念

DCM middleware family welcomes a new member

Distributed id solution

DCM 中间件家族迎来新成员

In PLC communication error or timeout or download the prompt solution of the model

MySQL row-level locks (row locks, adjacent key locks, gap locks)

文件包含漏洞

Data Persistence Technology - MP
随机推荐
How MySQL's allowMultiQueries flag relates to JDBC and jOOQ
科学论文和学术论文写作
ipv4和ipv6对比(IPV4)
建情人节表白网站(超详细过程,包教包会)
ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动
Chrome开发自定义右键菜单实现快速跳转到指定页面
5 open source Rust web development frameworks, which one do you choose?
Quickly learn database management
全动力学约束的机器人高效时间最优轨迹规划
DCM 中间件家族迎来新成员
Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
MySQL row-level locks (row locks, adjacent key locks, gap locks)
【Shader】Shader官方示例[通俗易懂]
Selenium自动化测试之Selenium IDE
最长算术(暑假每日一题 11)
Structural controllability of switched linear systems with symmetry constraints
CWE4.8 -- 2022年危害最大的25种软件安全问题
JVS函数公式使用场景介绍
订song餐系统
After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again