简体   繁体   English

是否可以告诉WPF绑定异步响应PropertyChanged事件(低优先级)?

[英]Is it possible to tell the WPF binding to respond asynchronously to a PropertyChanged event (low priority)?

I've got a long list of items which I'd like to filter. 我有一长串要过滤的项目。 I've added an IsFiltered property to the view model in my list. 我已经向列表中的视图模型添加了一个IsFiltered属性。 Using an ItemContainerStyle I'm able to bind the ListViewItem's visibility to the IsFiltered property. 使用ItemContainerStyle,我可以将ListViewItem的可见性绑定到IsFiltered属性。

<ListView ItemsSource="{Binding Path=MyItems}">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
           <Style.Triggers>
              <DataTrigger Binding="{Binding Path=IsFiltered}" Value="True">
                 <Setter Property="Visibility" Value="Collapsed"/>
              </DataTrigger>
           </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

All works well and changing the IsFiltered property properly reflects on the UI. 一切正常,更改IsFiltered属性可正确反映在UI上。 I'm running a background process to determine which items to filter and when it completes the IsFiltered property of each of the view model items gets updated on the UI thread. 我正在运行一个后台进程,以确定要筛选的项目以及何时完成筛选的每个视图模型项目的IsFiltered属性在UI线程上进行更新。 The problem is that due to the high number of PropertyChanged events being fired there is a very noticeable pause. 问题在于,由于触发了大量PropertyChanged事件,因此出现了非常明显的暂停。 I've managed to alleviate this by using 我设法通过使用

public bool IsFiltered
{
   get
   {
       return m_IsFiltered;
   }
   set
   {
       if (m_IsFiltered == value)
       {
           return;
       }
       m_IsFiltered = value;
       Dispatcher.CurrentDispatcher.BeginInvoke(
           DispatcherPriority.Background,
           (System.Action)(() => RaisePropertyChanged("IsFiltered")));
    }
}

in the view model. 在视图模型中。 The RaisePropertyChanged simply raises the PropertyChanged event. RaisePropertyChanged仅引发PropertyChanged事件。

I'm try to restrict knowledge of the UI framework (WPF) from the view model. 我试图从视图模型限制对UI框架(WPF)的了解。 In this case I'd like to just have the RaisePropertyChanged call and somehow give the UI the responsibility to listen to the property change in an asynchronous manner. 在这种情况下,我只想进行RaisePropertyChanged调用,并以某种方式赋予UI以异步方式监听属性更改的责任。 Is this possible? 这可能吗? It isn't very critical that the event gets handled as the property changed gets fired. 在更改属性后触发事件来处理事件并不是很关键。

I've tried adding IsAsync=True to the DataTrigger's binding, but this does not have the intended effect. 我尝试将IsAsync = True添加到DataTrigger的绑定中,但这没有达到预期的效果。

IsAsync is good, but you should determine correctly which one of your property change (caused by assignement of new data) require long running task, then put it there. IsAsync很好,但是您应该正确地确定哪个属性更改(由分配新数据引起)需要长时间运行的任务,然后将其放置在那里。 CMIIW IsAsync on your data trigger binding didn't work because to to perform changes on IsFiltered is only a simple task don't require long running task. 数据触发器绑定上的CMIIW IsAsync不起作用,因为要对IsFiltered执行更改只是一个简单的任务,不需要长时间运行的任务。

So find out which one of the property change possibly took long running task put the IsAsync = True there. 因此,找出哪个属性更改可能花费了长时间运行的任务,然后将 IsAsync = True 放在了 那里。

Hope that help. 希望对您有所帮助。

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

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