对象引用未设置为WPF组合框中的对象实例 [英] Object reference not set to an instance of an object in WPF combo box

查看:61
本文介绍了对象引用未设置为WPF组合框中的对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试在组合框中访问新添加的值时,它会给出此错误消息!



对象引用未设置为对象的实例



窗口有一个组合框和文本框!单击添加新项按钮时,应将文本框值加载到组合框中。这个加载的动作工作正常!但是当我们尝试访问新添加的一个时(例如点击编辑按钮时),显示上面的错误信息。



XMAL代码

When try to access newly added value in the combo box it gives this error message!

Object reference not set to an instance of an object

window have one combo box and text box! when click the Add new item button, text box value should be loaded in to the combo box.! This loaded action working fine! but when we try to access the newly added one( for example when click edit button) , above error message displayed.

XMAL Code

<ComboBox x:Name="cmbList"  x:FieldModifier="public" HorizontalAlignment="Left" Margin="58,10,0,0" VerticalAlignment="Top" Width="147" 

                    <ComboBoxItem Content="1st Item"/>
                    <ComboBoxItem Content="2nd Item"/>
                    <ComboBoxItem Content="3rd Item"/>
 </ComboBox>





按钮点击功能



button click function

cmbList.Items.Add(textbox1.text)





我尝试过:



Up添加新项目,它正常工作!如果我尝试重新选择它会给出此错误消息!



请有人纠正我!



谢谢提前!



What I have tried:

Up to add new item ,it working properly! If I try to re select it gives this error message!

Please anyone correct me!

Thanks in Advance!

推荐答案

使用调试器逐步执行代码,看看哪两个对象不包含有效的引用。



为了完整性,我只是在一个简单的WPF应用程序中尝试了该代码,它可以正常工作。因此,您的代码必须与您发布的代码不同。
Use your debugger to step through the code and see which of the two objects does not contain a valid reference.

For completeness I just tried that code in a simple WPF application and it works correctly. So your code must be different to what you have posted.


这篇关于对象引用未设置为WPF组合框中的对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆