2017-07-31 22 views
0

我有两个combobox的标准研究,它工作正常。 我有一个按钮显示全部:清除组合框和数据网格显示所有元素。在WPF中搜索后清除组合框?

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

1)在不选择在组合框(元件只是dispaly数据网格):我在数据网格6层的元件,它被correct..and组合框为空: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"); 
     } 
    } 

回答

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; 
    } 

它工作正常