在WPF中搜索后清除ComboBox?

时间:2017-07-31 17:04:57

标签: c# wpf mvvm

我有两个组合框的标准研究,它工作正常。 我有一个按钮全部显示:清除Combobox和DataGrid再次显示所有元素。

问题是:当我单击Button Dispaly All时,Combobox必须为空,前两个测试是正确的(Combobox是空的...... Datagrid显示所有元素),之后...我有两个Combobox的列表是空的!!

1)没有在Combobox中选择一个元素(只是显示数据网格):我在数据网格中有6个元素,它是正确的......并且组合框是空的:enter image description here

2) 选择搜索条件后(在Combobox中选择两个元素),我的结果是正确的:(例如:我只有3个结果,这是正确的动作)

3) 当我点击Display All按钮(两次测试正确后):Combobox中的列表为空! : enter image description here

XAML:

   <Window x:Class="WPFAuthentification.Views.BusinesseventsView"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" >   

     <Label Content="Entity Type" />
     <ComboBox Name="comboCodeType"
     ItemsSource="{Binding EntityLevelEnum}" 
     SelectedItem="{Binding EntityType, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, TargetNullValue=''}"
     />

     <Label Content="Criticality" />
     <ComboBox Name="comboType" 
        ItemsSource="{Binding LevelCriticalityEnum}" 
        SelectedItem="{Binding Criticality, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, TargetNullValue=''}" />



    <Button Content="Dislplay all" ToolTip="Display All Business Events" 
            VerticalAlignment="Top"  Command="{Binding Initialize}"  />

     <DataGrid Name="businesseventsalarms" 
                ItemsSource="{Binding BusinessEventsList}" SelectedItem="{Binding SelectedBusinessevent}" ... >
    </Window>

视图模型:

    public BusinesseventsViewModel()
    {
        businessEventsList = new ObservableCollection<BusinessEventClass>(WCFclient.getAllBusinessEvent());
        levelCriticalityEnum = new ObservableCollection<Level_Criticality>(Enum.GetValues(typeof(Level_Criticality)).Cast<Level_Criticality>());
        entityLevelEnum = new ObservableCollection<BusinessEntityLevel>(Enum.GetValues(typeof(BusinessEntityLevel)).Cast<BusinessEntityLevel>());

        //Function of Button Display All
        initialize = new RelayCommand<string>(initFunc);    
   }        
    private void initFunc(object obj)
    {
        entityLevelEnum.Clear();          
        levelCriticalityEnum.Clear();          
        OnPropertyChanged("EntityLevelEnum");
        OnPropertyChanged("LevelCriticalityEnum");     
    }   

    private string  entityType;
    public string EntityType
    {
        get { return entityType; }
        set
        {
            entityType = value;
            businessEventsList = filterByCriteria(entityType, criticality);
            OnPropertyChanged("BusinessEventsList");      
            OnPropertyChanged("EntityType");


        }
    }     
    private string criticality;
    public string Criticality
    {
        get { return criticality; }
        set
        {
            criticality = value;
            businessEventsList = filterByCriteria(entityType, criticality);
            OnPropertyChanged("BusinessEventsList");
            OnPropertyChanged("Criticality");                
        }
    }  

     public ObservableCollection<BusinessEventClass> filterByCriteria(string entityType, string criticality)
    {

        BusinessEventsList = new ObservableCollection<BusinessEventClass>(WCFclient.getAllBusinessEvent());     

        ObservableCollection<BusinessEventClass> updatedList = new ObservableCollection<BusinessEventClass>();


        if ((entityType != null && entityType != "") && (Criticality != null))
            {
                updatedList = new ObservableCollection<BusinessEventClass>(BusinessEventsList.Where(a => a.EntityType.ToString().ToLower().Equals(criticality)
                                                                         && a.Critciality.ToString().Equals(criticality));
            }

                return updatedList;                    
          }  

    } 

        //List of all BusinessEvents 
    public ObservableCollection<BusinessEventClass> BusinessEventsList
    {
        get { return businessEventsList; }
        set
        {
            businessEventsList = value;
            OnPropertyChanged("BusinessEventsList");
        }
    }

    //List of LevelCriticalityEnum
    private ObservableCollection<Level_Criticality> levelCriticalityEnum;
    public ObservableCollection<Level_Criticality> LevelCriticalityEnum
    {
        get { return levelCriticalityEnum; }
        set
        {
            levelCriticalityEnum = value;
            OnPropertyChanged("TypeEnum");
        }
    }

    //List of EntityLevelEnum
    private ObservableCollection<BusinessEntityLevel> entityLevelEnum;
    public ObservableCollection<BusinessEntityLevel> EntityLevelEnum
    {
        get { return entityLevelEnum; }
        set
        {
            entityLevelEnum = value;
            OnPropertyChanged("TypeEnum");
        }
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:修改后面代码中的代码

private void DisplayAll_Clickk(object sender, RoutedEventArgs e)
    {   //comboCodeType : name of combobox for the EntityType
        comboCodeType.SelectedIndex = -1;
         //comboType: name of combobox for the Criticality
        comboType.SelectedIndex = -1;
    }

工作正常