可观察的集合过滤器WPF和过滤dat的不同方式

时间:2017-09-21 05:21:56

标签: c# wpf data-binding

我正在开发一个WPF应用程序,其中我遵循MVVM模型,我试图过滤可观察集合但是这个方法没有返回任何值public void UpdatePopList(),这个代码是用正确的方式编写的还是我需要一些修改还有什么不同的方法可以过滤数据吗?

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>

1 个答案:

答案 0 :(得分: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/