且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Wpf xaml当我点击编辑它时检查组合框值是否为空。如果value不为null,如何禁用组合框

更新时间:2023-11-29 10:34:52

你是什么意思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();
}
}





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


 <   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