[英]wpf fire PropertyChanged event in binding source that binding gets updated
[英]Is it possible to tell the WPF binding to respond asynchronously to a PropertyChanged event (low priority)?
我有一長串要過濾的項目。 我已經向列表中的視圖模型添加了一個IsFiltered屬性。 使用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>
一切正常,更改IsFiltered屬性可正確反映在UI上。 我正在運行一個后台進程,以確定要篩選的項目以及何時完成篩選的每個視圖模型項目的IsFiltered屬性在UI線程上進行更新。 問題在於,由於觸發了大量PropertyChanged事件,因此出現了非常明顯的暫停。 我設法通過使用
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")));
}
}
在視圖模型中。 RaisePropertyChanged僅引發PropertyChanged事件。
我試圖從視圖模型限制對UI框架(WPF)的了解。 在這種情況下,我只想進行RaisePropertyChanged調用,並以某種方式賦予UI以異步方式監聽屬性更改的責任。 這可能嗎? 在更改屬性后觸發事件來處理事件並不是很關鍵。
我嘗試將IsAsync = True添加到DataTrigger的綁定中,但這沒有達到預期的效果。
IsAsync
很好,但是您應該正確地確定哪個屬性更改(由分配新數據引起)需要長時間運行的任務,然后將其放置在那里。 數據觸發器綁定上的CMIIW IsAsync
不起作用,因為要對IsFiltered
執行更改只是一個簡單的任務,不需要長時間運行的任務。
因此,找出哪個屬性更改可能花費了長時間運行的任務,然后將 IsAsync = True
放在了 那里。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.