可观察的收集过滤器WPF和不同的方式来过滤数据

问题描述:

我正在开发一个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

的问题,就是你查询你的数据,并将它们存储在不同的表。

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