如何在研究WPF后清除ComboBox?

时间:2017-07-27 09:32:25

标签: c# wpf

我根据两个组合框的标准进行了研究,它运行良好 研究完成后,我有一个按钮全部显示:将组合框重置为null ..以及所有元素的DataGrid显示,

单击Button Dispaly All时组合框必须为空的问题!

  • 没有在组合框中选择一个元素(只是显示数据网格):我在数据网格中有6个元素,它是正确的...组合框是空的

  • 选择搜索条件后,我的结果是正确的:(我只有3个结果,这是正确的操作) 3 elements picture

  • 当我点击按钮全部显示:(我拥有数据网格中的所有元素,6个元素......这是正确的)但是Combobox不是空的! 6 elements picture

观点:

 <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" Width="128" Grid.Row="1" Grid.ColumnSpan="2"/>
     <ComboBox HorizontalAlignment="Center" VerticalAlignment="Center"  
         ItemsSource="{Binding EntityLevelEnum}" 
        SelectedItem="{Binding EntityType, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True, TargetNullValue=''}"
        Grid.ColumnSpan="2" Grid.Column="1"  />


        <Button Content="Dislplay all" ToolTip="Display All Business Events" 
                VerticalAlignment="Top"  Command="{Binding Initialize}" 
                Visibility="{Binding Path=ShowDisplayAllButton, Converter={StaticResource BoolToVis}}"  />

         <DataGrid ..... />
</Window>

ViewModel:

class BusinesseventsViewModel : ViewModelBase1
    {

        private ObservableCollection<BusinessEventClass> businessEventsList;    

        private RelayCommand<string> initialize;
        public RelayCommand<string> Initialize
        {
            get { return initialize; }
        }       
        public BusinesseventsViewModel()
        {
            //businessEventsList: to Get all the Business events
            businessEventsList = new ObservableCollection<BusinessEventClass>(WCFclient.getAllBusinessEvent());
            //Enumeration of Entity Type and Criticality
            levelCriticalityEnum = new ObservableCollection<Level_Criticality>(Enum.GetValues(typeof(Level_Criticality)).Cast<Level_Criticality>());
            entityLevelEnum = new ObservableCollection<BusinessEntityLevel>(Enum.GetValues(typeof(BusinessEntityLevel)).Cast<BusinessEntityLevel>());
            //the Button Display All :
            initialize = new RelayCommand<string>(initFunc);    
       }        

       //Function of the Button Display All
        private void initFunc(object obj)
        {
            EntityType = null;
            OnPropertyChanged("EntityLevelEnum");
            Criticality = null;
            OnPropertyChanged("Criticality");
        }     

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

             //Function of the research :
                 public ObservableCollection<BusinessEventClass> filterByCriteria(string entityType, string criticality)
        {               
            BusinessEventsList = new ObservableCollection<BusinessEventClass>(WCFclient.getAllBusinessEvent());         

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

            if ((entityType == null) && (Criticality == null))
            {
                updatedList = businessEventsList;
            }          

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

        }

1 个答案:

答案 0 :(得分:0)

您的INotifyPropertyChanged接口的实现一定有问题。

使用GalaSoft.MvvmLight我做了这个,并且没有问题;

Window.cs内容:

theta2

和我的Window.xaml内容

public partial class MainWindow
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new TestViewModel();
    }
}

public class TestViewModel : ViewModelBase
{
    private string _selectedItem;


    public RelayCommand Command { get; }
    public ObservableCollection<string> ItemsSource { get; }

    public string SelectedItem
    {
        get { return _selectedItem; }
        set { Set(ref _selectedItem, value); }
    }

    public TestViewModel()
    {
        Command = new RelayCommand(() => SelectedItem = null);
        ItemsSource = new ObservableCollection<string> { "index 0", "index 1", "index 2", "index 3" };
    }
}

测试并按预期工作(当您将null作为SelectedItem时,组合框返回空)