で、ComboBoxのドロップダウンメニューを出したうえで画面を終了すると、裏でバインドエラーが出るようになった。
なんぞこれ?しかも質の悪いことに、コンボボックス内に表示したアイテム分発生している。100件のアイテムがあったら、上のエラー100個、合計200個のエラーが出ている事になる。このバインディングエラーというのが曲者で、パフォーマンスに悪影響を与えるものだ。調べてみたら、結構あった。System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
Resolving harmless binding errors in WPF
Combobox throwing Data Binding Error
ListBoxItem produces “System.Windows.Data Error: 4” binding error
最初のリンクにあった内容を引用すると
ComboBoxだけでなく、ListBox等、内容が動的に変わるコントロールに発生するらしい。解決策としては、Style設定をする事だそうだ。This is a "known" issue, and happens to all controls that contain dynamically created lists (all item controls i.e. ComboBox, menu, ListBox etc.). ControlTemplate of items in these controls (specifically MenuItem, ComboBoxItem etc.) try to find the nearest ItemsControl and bind to the VerticalAlignment and HorizonalAlignment properties and raises this error on not finding the source.
<!-- ComboBox For Resolving Binding Error-->ま、解決策があるだけましか。.NET4.5でも試してみないとな。
<Style TargetType="ComboBoxItem">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>