2017-07-27 47 views
0

我不得不準則的研究與兩個組合框,它工作正常 研究結束後,我有一個按鈕顯示全部:重置組合框null..and與所有元素的DataGrid中顯示,如何在研究WPF後清除組合框?

的當點擊Button Dispaly All時,組合框必須爲空的問題!

  • 在不選擇在組合框的元件(只是dispaly數據網格):我在數據網格6層的元件,它被correct..and組合框爲空

  • 選擇搜索條件後,我有結果正確:(我只有3個結果,這是正確的行動) 3 elements picture

  • 當我點擊按鈕顯示所有:(我在數據網格的所有元素,6 elements..It是正確的)但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> 

視圖模型:

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

     } 
+0

你永遠不會清除entityLevelEnum? – GCamel

+0

謝謝,我添加EntityLevelEnum = null; OnPropertyChanged(「EntityLevelEnum」);它的工作原理,組合框是空的... 但之後,當我想選擇Combbox中的新元素.. EntityLevelEnum的組合框仍空! – TunNet

回答

0

一定有什麼不對您實現INotifyPropertyChanged接口的。

使用GalaSoft.MvvmLight我做到了這一點,工作沒有問題;

Window.cs內容:

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

和我Window.xaml內容

<Window.Resources> 
    <ResourceDictionary> 
     <DataTemplate DataType="{x:Type testClean:TestViewModel}"> 
      <Grid> 
       <Viewbox> 
        <TextBlock Foreground="HotPink">just some pink text</TextBlock> 
       </Viewbox> 
       <ComboBox Height="50" Width="200" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="20" SelectedIndex="0" 
          ItemsSource="{Binding ItemsSource}" 
          SelectedItem="{Binding SelectedItem}"/> 
       <Button Command="{Binding Command}" Height="50" Width="100" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="250,20,20,20">Reset</Button> 
      </Grid> 
     </DataTemplate> 
    </ResourceDictionary> 
</Window.Resources> 

<ContentControl Content="{Binding}" /> 

測試並且按照預期工作(當你把null作爲的SelectedItem的組合框返回空)

+0

謝謝, 但我在我的實現中做了同樣的事情,只是SelectedItem我將它重命名爲EntityType .. 我想讓組合框爲空的問題..然後當我可以從此組合框的列表中選擇一個新項目例如,嘗試創建一個新項目,例如 – TunNet

+0

。 從nuget添加GalaSoft.MVVMLight並嘗試使用此代碼,它按預期工作(並且您想要) – Easly

+0

非常感謝您的幫助, 我會嘗試 – TunNet