绑定到ObservableCollection来显示前X个数的项目c# WPF

本文关键字:项目 WPF ObservableCollection 显示 绑定 | 更新日期: 2023-09-27 18:18:51

背景说明

现在我将ContextMenu ItemsSource绑定到ObservableCollection比如TypeA

以下代码位于单例类DataStore

private ObservableCollection<TypeA> TypeACollection = new ObservableCollection<TypeA>();
public ObservableCollection<TypeA> GetTypeACollection
{
    get { return TypeACollection; }
}
public ObservableCollection<TypeA> GetFirstFiveTypeACollection
{
    get 
    {
        return TypeACollection.Take(5);
    }
}
现在我已经成功地绑定了ItemsControlGetTypeACollection与以下XAML代码: 下面的代码位于MainWindow.xaml
<ItemsControl x:Name="TypeAList" ItemsSource="{Binding GetTypeACollection, Source={StaticResource DataStore}, UpdateSourceTrigger=PropertyChanged}" >
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <VirtualizingStackPanel />
    </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <User_Controls:TypeAUserControl Type="{Binding }"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

按预期工作,显示TypeAUserControl,正确地格式化了TypeA

中的数据

现在,当我尝试通过将ItemsSource绑定到GetFirstFiveTypeACollection来在ContextMenu MenuItem上重复此操作时,我最初看到预期的结果,但是在删除TypeA对象时,主窗口ItemsControl被更新,而ContextMenu不是。

我相信这是由于绑定本身在ContextMenu和"新"ObservableCollection<TypeA>之间(如GetFirstFiveTypeAColletion所示)。

我也尝试过使用IValueConverter

以下代码位于ValueConverters

类中
public class ObservableCollectionTypeAResizeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        ObservableCollection<TypeA> TypeACollection = value as ObservableCollection<TypeA>;
        return TypeACollection.Take(System.Convert.ToInt32(parameter));
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return null;
    }
}

我尝试过的XAML代码。

使用IValueConverter

<MenuItem Header="First 5 Type A" Name="MI_FirstFiveTypeA" 
    ItemsSource="{Binding DATA_STORE.GetTypeACollection, ConverterParameter=5,
            Converter={StaticResource ObservableCollectionTypeAResizeConverter}, 
            Source={StaticResource DataStore}}" 
/>
使用GetFirstFiveTypeACollection

<MenuItem Header="First 5 Type A" Name="MI_FirstFiveTypeA" 
    ItemsSource="{Binding DATA_STORE.RecentTimers, Source={StaticResource DataStore}, 
            UpdateSourceTrigger=PropertyChanged}"
/>

我不知道下一步该怎么做,或者我应该怎么做,任何帮助都会非常感激!

<标题>编辑

好的,我修改了下面的

DataStore.cs

private ObservableCollection<TimerType> TimerTypes = new ObservableCollection<TimerType>();
public ObservableCollection<TimerType> getTimerTypes
{
    get 
    { 
        return new ObservableCollection<TimerType>(TimerTypes.OrderByDescending(t => t.LastUsed)); 
    }
}
public ObservableCollection<TimerType> RecentTimers
{
    get 
    { 
        return new ObservableCollection<TimerType>(TimerTypes.OrderByDescending(t => t.LastUsed).Take(5)); 
    }
}
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
//THIS IS IN THE ADD METHOD
TimerTypes.Add(timer);
NotifyPropertyChanged("getTimerTypes");
NotifyPropertyChanged("RecentTimers");
NotifyPropertyChanged("TimerTypes");
//THIS IS IN THE REMOVE METHOD
TimerTypes.Remove(Timer);
NotifyPropertyChanged("getTimerTypes");
NotifyPropertyChanged("RecentTimers");
NotifyPropertyChanged("TimerTypes");

MainWindow.xaml

<ItemsControl x:Name="TimersList" ItemsSource="{Binding Path=getTimerTypes, UpdateSourceTrigger=PropertyChanged}" >

MainWindow.xaml.cs

//Lists are populated in DataStore.cs before this.
DataContext = DataStore.DATA_STORE;
InitializeComponent();

NotifyIcon.xaml

<MenuItem Header="Recent Timers" Name="MIRecent" ItemsSource="{Binding RecentTimers, UpdateSourceTrigger=PropertyChanged}"/>

NotifyIcon.xaml.cs

DataContext = DataStore.DATA_STORE;
InitializeComponent();

所以一切都在开始时正确绑定,但当TimerType被删除时,PropertyChangedEventHandler总是NULL。我认为这是一个DataContext的问题,但我很确定我有DataContext和所有的Bindings现在正确吗?

创建单例实例

private static readonly DataStore Instance = new DataStore();
private DataStore() { }
public static DataStore DATA_STORE
{
    get { return Instance; }
    set { }
}

绑定到ObservableCollection来显示前X个数的项目c# WPF

不幸的是,通过使用Take(或任何LINQ方法),您可以直接返回IEnumerable。当你绑定到它时,没有INotifyCollectionChanged,所以对集合的更改不会导致UI更新。

由于缺乏INotifyCollectionChanged,没有真正的解决方案。您最好的选择是在添加/删除项目时针对"子集"属性提高PropertyChanged,以强制UI重新计算IEnumerable

您可以使用CollectionView

来解决这个问题
public class MyViewModel
{
    CollectionView _mostRecentDocuments;
    public MyViewModel()
    {
        Documents = new ObservableCollection<Document>();
        _mostRecentDocuments = new CollectionView(Documents);
        _mostRecentDocuments .Filter = x => Documents.Take(5).Contains(x);
    }
    public ObservableCollection<Document> Documents { get; private set; }    
    public CollectionView MostRecentDocuments
    {
        get 
        {
            return _mostRecentDocuments;
        }
    }
}