【问题标题】:observable collection filters WPF and different way to filters the dat可观察的收集过滤器 WPF 和过滤数据的不同方式
【发布时间】:2017-09-21 05:21:55
【问题描述】:

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

【问题讨论】:

  • 我认为在 UI 中您绑定了“CRmappings2”,但您正在过滤实体类。它应该是您应该过滤的 ObservableCollection。
  • @Eldho 是的,你是对的,我将其更改为过滤可观察的集合,使其像这样 CRPopenities = CRmappings2.Where(p => p.MU_Identifier == selectmu).ToList(); --- 根据查询,它应该有多条记录吗?它唯一返回的单条记录,这是因为我使用的是 ToList() 吗?
  • 我认为它不包含带有MU_Identifier 的多条记录。 ToList() 是正确的。你可以调试它。
  • @Eldho 我调试了代码,它返回了 3 条记录,但在 UI 上它什么也没显示。我想我缺少绑定部分。
  • @Eldho 这是我的绑定代码

标签: c# wpf data-binding


【解决方案1】:

问题是您查询您的数据并将它们存储在不同的列表中。

    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/

【讨论】:

  • 感谢您的输入,但这不起作用,因为 CRPopenities 是空列表,我从 @Eldho 那里得到了正确的解决方案,我必须像这样更改它 --- CRPopenities = CRmappings2。 Where(p => p.MU_Identifier == selectmu).ToList();
猜你喜欢
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 2016-11-19
  • 2012-03-25
  • 2018-12-22
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多