2017-09-21 20 views
0

我正在開發一個WPF應用程序,其中我遵循MVVM模型,我想過濾可觀察集合,但此方法不返回任何值public void UpdatePopList()這個代碼以正確的方式寫入,或者我需要一些修改,也有任何不同的方式來過濾數據?可觀察的收集過濾器WPF和不同的方式來過濾數據

private string selectmu; 
    public string Selectmu 
    { 
     get 
     { 
      return selectmu; 
     } 
     set 
     { 
      selectmu = value; 
      RaisePropertyChanged("Selectmu"); 
     } 
    } 

    private ObservableCollection<CREntity> _CRmappings2 = new ObservableCollection<CREntity>(); 

    public List<CREntity> CRPopentities 
    { 
     get; 
     set; 
    } 

    // Obeservable collection property for access 
    public ObservableCollection<CREntity> CRmappings2 
    { 
     get { return _CRmappings2; } 
     set 
     { 
      _CRmappings2 = value; 
      RaisePropertyChanged("CRmappings2"); 
     } 
    } 

    public void UpdatePopList() 
    { 
     CRPopentities = CRPopentities.Where(p => p.MU_Identifier == selectmu).ToList(); 
    } 
} 

此UI綁定代碼

      <md:PopupBox.ToggleContent> 
            <md:PackIcon Kind="DotsHorizontal" Margin="4 0 4 0" Width="24" Height="24" 
       Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=md:PopupBox}, Path=Foreground}" /> 
           </md:PopupBox.ToggleContent> 
          <i:Interaction.Triggers> 
           <i:EventTrigger EventName="Opened"> 
            <command:EventToCommand Command="{Binding DataContext.popSW, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}" CommandParameter="{Binding MU_Identifier}" /> 
           </i:EventTrigger> 
          </i:Interaction.Triggers> 
          <!--<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding ElementName=CRDataGrid, Path= SelectedItem.MU_Identifier}" />--> 
          <DataGrid x:Name="dataGrid1" Grid.Column="1" Grid.Row="2" AutoGenerateColumns="False" ItemsSource="{Binding Path=CRPopentities, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" > 
           <DataGrid.Columns> 
            <DataGridTextColumn Header="Software Versions" Binding="{Binding Path=SW_Version}" ></DataGridTextColumn> 
           </DataGrid.Columns> 
          </DataGrid> 
         </md:PopupBox> 
+0

我想在UI您綁定「CRmappings2」,但要篩選的實體類。它應該是ObservableCollection,你應該過濾。 – Eldho

+0

@Eldho是的你是對的,我改變它過濾可觀察的集合,以便這樣CRPopentities = CRmappings2.Where(p => p.MU_Identifier == selectmu).ToList(); ---根據查詢它應該多個記錄對嗎?它唯一的返回單個記錄,這是因爲我使用ToList()? –

+0

我覺得它不包含'MU_Identifier'的多個記錄。 ToList()是正確的。你可以調試它。 – Eldho

回答

0

的問題,就是你查詢你的數據,並將它們存儲在不同的表。

public void UpdatePopList() 
    { 
     CRPopentities = CRPopentities.Where(p => p.MU_Identifier == selectmu).ToList(); 
     CRmappings2.Clear(); 
     foreach (var item in CRPopentities) 
     { 
      CRmappings2.Add(item); 
     } 

    } 

一般來說,我認爲你應該嘗試命名你的變量更清晰。

也許這可以幫助你 http://www.c-sharpcorner.com/UploadFile/8a67c0/C-Sharp-coding-standards-and-naming-conventions/

+0

感謝您的輸入,但這並不工作,因爲CRPopentities是空列表,我從@Eldho得到了正確的解決方案,我不得不像這樣改變它--- CRPopentities = CRmappings2.Where(p => p.MU_Identifier == selectmu).ToList(); –