简体   繁体   English

线程安全的CollectionViewSource

[英]Thread-safe CollectionViewSource

I've been using Caliburn.Micro's BindableCollection to render the data for Telerik RadGridView and it's thread-safe which means I can update the collection from the non-UI thread. 我一直在使用Caliburn.Micro的BindableCollection渲染Telerik RadGridView的数据,并且它是线程安全的,这意味着我可以从非UI线程更新集合。

Now what I am trying to do is to filter the datagrid using a predicate. 现在,我想做的是使用谓词过滤datagrid。 The nature thought is to use CollectionViewSource so that my underlying data does not change and I can control the CollectionViewSource's view by applying predicate on it. 自然的想法是使用CollectionViewSource,这样我的基础数据就不会改变,并且我可以通过在其上应用谓词来控制CollectionViewSource的视图。 The issue is CollectionViewSource is not thread-safe and will throw if the source updated from non-UI thread. 问题是CollectionViewSource不是线程安全的,如果源是从非UI线程更新的,则会抛出该异常。

Here below is the XAML and view model. 以下是XAML和视图模型。

XAML: XAML:

        <controls:RadGridView Grid.Column="2"
                          AutoGenerateColumns="False"
                          behaviours:RadGridViewExtensions.RowDoubleClick="OpenProgram"
                          CanUserDeleteRows="False"
                          CanUserInsertRows="False"
                          DataContext="{Binding ContractHeadersCollectionView}"
                          IsReadOnly="True"
                          ItemsSource="{Binding}"
                          RowIndicatorVisibility="Collapsed"
                          SelectedItem="{Binding SelectedContractHeader}"
                          SelectionMode="Single"/>

View Model: 查看模型:

    /// <summary>
    /// Initialises a new instance of the <see cref="PortfolioViewModel"/> class.
    /// </summary>
    public PortfolioViewModel()
    {
        this.ContractHeaders = new BindableCollection<ContractHeaderViewModel>();
        this.ContractHeadersCollectionView = new CollectionViewSource() { Source = ContractHeaders };
    }

    public CollectionViewSource ContractHeadersCollectionView { get; private set; }

The questions are: 问题是:

  1. Does Calibun.Micro provide a thread-safe CollectionViewSource equivalent? Calibun.Micro是否提供等效的线程安全CollectionViewSource?
  2. If not, what's the option if I don't want marshal the calls to update BindableCollection to the UI thread? 如果没有,如果我不希望封送将BindableCollection更新为UI线程的调用,该怎么办?

Thanks for any thoughts or suggestions. 感谢您的任何想法或建议。

You need to marshal calls to the UI thread. 您需要封送对UI线程的调用。 Caliburn.Micro provides Execute.OnUiThread in Caliburn.Micro for marshalling calls to the UI Thread. Caliburn.Micro在Caliburn.Micro中提供Execute.OnUiThread ,用于编组对UI线程的调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM