如何在WPF中选择列表框值时禁用控件(组合Bx,文本Bx) [英] How to disable the controls(Combo bx,Text Bx) on selection of List box values in wpf

查看:83
本文介绍了如何在WPF中选择列表框值时禁用控件(组合Bx,文本Bx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

在我的WPF应用程序中,我没有几个控件,即列表框(A)和组合框(B)和文本框(C),我需要禁用实体,即,如果用户从列表框选择(A)任何值, ComboBox和TextBox中的两项均应被禁用,反之亦然.

对于实例,使用Listbox的SelectionChanged属性,旨在使用下面的以下代码禁用其他两个控件(即ComboBx,TextBx).和xaml代码

//MyFile.xaml

Dear All,

In my WPF Application, I have few controls viz., List Box(A) and ComboBox(B) and TextBox(C), I need to disable the entities, ie., If User selects (A) any value from List Box, Both items in ComboBox and TextBox should be disabled and vice versa.

For Instance,using SelectionChanged property for Listbox, intended to disable two other controls(viz., ComboBx, TextBx) using the below code behind. and xaml code

//MyFile.xaml

<ListBox Name="MyListBox"  SelectionChanged="ListBx_SelectionChanged"  ...>
.....



//MyFile.xaml.cs



//MyFile.xaml.cs

private void ListBx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    MyTextbx.IsEnabled = false;
    MyComboBx.IsEnabled = false;
}



我对上述更改有主张,请让我知道我们可以解决相同的问题?或任何其他替代方法来解决上述问题.发生类型为"System.NullReferenceException"的异常,并且未在用户代码中处理.附加信息:对象引用未设置为对象的实例.


解决上述问题的任何帮助都是可以理解的.



I get assertion on above change, Please let me know we can resolve the same? or any other alternative approach to target the above problem. The Exception of type "System.NullReferenceException" occurred, and was not handled in user code. Additional Information: Object Reference not set to an Instance of an Object.


Any Help in resolving the above would be appreciable.

推荐答案

处理ListBx_SelectionChanged时,可能尚未初始化MyTextbx和MyComboBx.
尝试检查MyTextbx和MyComboBx是否为空
Probably MyTextbx and MyComboBx not been yet initialized when the ListBx_SelectionChanged is handled.
Try to check MyTextbx and MyComboBx for null
private void ListBx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  if(MyTextbx!=null && MyComboBx!=null)
   {
    MyTextbx.IsEnabled = false;
    MyComboBx.IsEnabled = false;
   }
}


这篇关于如何在WPF中选择列表框值时禁用控件(组合Bx,文本Bx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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