【发布时间】: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