Wpf xaml当我点击编辑它时检查组合框值是否为空。如果value不为null,如何禁用组合框 [英] Wpf xaml when I click edit it checks if combobox value is null or not. How to disable combobox if value is not null

查看:100
本文介绍了Wpf xaml当我点击编辑它时检查组合框值是否为空。如果value不为null,如何禁用组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  private  CarType _inspectMethod; 

public CarType InspectMethod
{

get
{
return _inspectMethod;
}
set
{

_inspectMethod = value ;
RaisePropertyChanged();

if (InspectMethod!= null
{
InspectTypelist = InspectMethod.InspectType;
}
}

< Label Grid.Row = 0 Grid.Column = 0 Content = value: />
< ComboBox Width = Auto Grid.Row = 4 Grid.Column = 1 SelectedValuePath = Id
DisplayMemberPath = < span class =code-string>
名称 SelectedItem = {Binding ElementName = LayoutRoot,
Path = DataContext.InspectMethod}
ItemsSource = {Binding
ElementName = LayoutRoot,Path = DataContext.InspectMethodList}
/>





我尝试过:



  bool  _isEnable; 
public bool IsEnable

{
get
{
return this ._ isEnable;
}

set
{
this ._ isEnable = value ;
}
}

< ComboBox Width = Auto Grid.Row = 4 Grid.Column = 1 IsEnable = 绑定一些东西 SelectedValuePath = Id
DisplayMemberPath = 名称 SelectedItem = {Binding ElementName = LayoutRoot,
Path = DataContext.InspectMethod}
ItemsSource = {绑定
ElementName = LayoutRoot,Path = DataContext.InspectMethodList}
/>

解决方案

< blockquote>你是什么意思combobox va略。你的意思是enable-diable组合框有没有项目列表。如果是这种情况,您可以使用以下代码



  public   class  ComboBoxEnableConverter:IValueConverter 
{
public object 转换( object value ,输入targetType,对象参数,CultureInfo文化)
{
bool hasItem =( bool value ;
if (hasItem)
return ;
return false ;
}
public object ConvertBack( object value ,输入targetType, object 参数,CultureInfo文化)
{
throw new NotImplementedException();
}
}





然后添加到资源并绑定到启用属性,如下所示< br $>


 <   UserControl .Resources  >  
< common:ComboBoxEnableConverter x:Key = EnableConverter / >
< / UserControl.Resources >

< ComboBox x:名称 = cmboBox IsEnabled = {Binding ElementName = cmboBox,Path = HasItems,Converter = {StaticResource EnableConverter}} / >







1.如果要将源值转换为目标,请定义转换器,例如,项目计数为布尔值(IsEnable on接受布尔值)

2.绑定IsEnable财产符合您的要求。在这个例子中,我想检查组合框是否在项目源中有一些项目。如果是,则Converter将返回true,否则为false


private CarType _inspectMethod;
    
    public CarType InspectMethod
    {
    
            get
            {
                return _inspectMethod;
            }
            set
            {
     
                _inspectMethod= value;
                RaisePropertyChanged();
                
             if(InspectMethod!= null)
                {                    
                    InspectTypelist = InspectMethod.InspectType;
                }
    }   

    <Label Grid.Row="0" Grid.Column="0" Content="value:" />
   <ComboBox Width="Auto" Grid.Row="4" Grid.Column="1" SelectedValuePath="Id" 
    DisplayMemberPath="Name" SelectedItem="{ Binding ElementName=LayoutRoot, 
    Path=DataContext.InspectMethod}" ItemsSource="{ Binding 
    ElementName=LayoutRoot, Path=DataContext.InspectMethodList}"/>



What I have tried:

bool _isEnable;
 public bool IsEnable

        {
            get
            {
                return this._isEnable;
            }

            set
            {
                this._isEnable = value;
            }
        }

 <ComboBox Width="Auto" Grid.Row="4" Grid.Column="1" IsEnable="Binding something here" SelectedValuePath="Id" 
    DisplayMemberPath="Name" SelectedItem="{ Binding ElementName=LayoutRoot, 
    Path=DataContext.InspectMethod}" ItemsSource="{ Binding 
    ElementName=LayoutRoot, Path=DataContext.InspectMethodList}"/>

解决方案

What do you mean by combobox value. do you mean enable-diable combo box has list of items or not. If this is the case, you can use following code

public class ComboBoxEnableConverter : IValueConverter
   {
       public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
       {
           bool hasItem = (bool)value;
           if (hasItem)
               return true;
           return false;
       }
       public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
       {
           throw new NotImplementedException();
       }
   }



Then Add to Resource and bind to Enable property as given below

<UserControl.Resources>
        <common:ComboBoxEnableConverter x:Key="EnableConverter"/>
</UserControl.Resources>

<ComboBox x:Name="cmboBox" IsEnabled="{Binding ElementName=cmboBox, Path=HasItems, Converter={StaticResource EnableConverter}}"/>




1. Define converter if you want to convert source value into target e.g items count to boolean (IsEnable on accepts boolean)
2. Bind IsEnable property to your requirement. In this example I want to check if combo box has some items in item source. if yes, Converter will return true, else false


这篇关于Wpf xaml当我点击编辑它时检查组合框值是否为空。如果value不为null,如何禁用组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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