[英]Cannot find source for binding with reference 'RelativeSource FindAncestor
[英]Prevent WPF Binding Exception: Cannot find source for binding with reference 'RelativeSource FindAncestor…' reduces performance
我寫的Style
:
<Style x:Key="ProductItemContainerStyle"
TargetType="{x:Type ListBoxItem}"
BasedOn="{StaticResource ProductItemContainerBaseStyle}">
<Setter Property="IsSelected"
Value="{Binding Path=IsExpanded, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Expander}}, Mode=OneWayToSource}" />
</Style>
僅在為包含此ListBoxItem
的ListBox
應用分組時才相關。 但是,大多數情況下它不在分組中,這導致數十,數百和數千個綁定異常(取決於列表中有多少項)。 綁定異常是性能問題的已知原因。 當后面的代碼選擇ListBoxItem
並且IsSelected
更改為true時,此綁定應擴展Expander
。 如您所見,綁定是Mode=OneWayToSource
。
有沒有辦法防止這些綁定異常?
僅在為包含此
ListBoxItem
的ListBox
應用分組時才相關...
然后僅應用它,即在您確實分組時使用不同的樣式,或者在觸發器中設置IsSelected
屬性,以確定您當前是否處於“分組”階段。
如果您將Setter
硬編碼為默認Style
,那么XAML處理器當然總是嘗試解析綁定。 告訴它不要的唯一方法是從XAML中刪除綁定。
有沒有辦法防止這些綁定異常?
執行此操作的唯一方法是刪除失敗的綁定。 您可以在工具 - >選項 - > WPF跟蹤設置 - >數據綁定下關閉跟蹤,但這不會阻止在XAML處理器嘗試解析綁定時實際拋出的消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.