我正在開發一個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>
我想在UI您綁定「CRmappings2」,但要篩選的實體類。它應該是ObservableCollection,你應該過濾。 – Eldho
@Eldho是的你是對的,我改變它過濾可觀察的集合,以便這樣CRPopentities = CRmappings2.Where(p => p.MU_Identifier == selectmu).ToList(); ---根據查詢它應該多個記錄對嗎?它唯一的返回單個記錄,這是因爲我使用ToList()? –
我覺得它不包含'MU_Identifier'的多個記錄。 ToList()是正確的。你可以調試它。 – Eldho