views:

1262

answers:

2

I'm working with a WPF dialog window that contains a ListBox. The list box can display both an 'icon' and 'list' view. To accomplish this, we used ListBoxItem styles that are dynamically applied to the ListBox, as well as a pair of corresponding ItemsPanelTemplates. As is, it displays beautifully. However, when loading this dialog we get a few dozen binding errors, such as the following:

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 'ListBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

Our theory is that the styles and templates are loading before the ListBox, and failing to find the properties they are bound to using code such as

This seems to happen one or more times before the ListBox is initialized and the styles and templates are applied. After the dialog finishes initializing, the errors stop.

Has anyone encountered this before? Can anyone suggest a solution we may have overlooked? Thanks!

A: 

Looks like this was covered here:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/db050ce6-d084-41ad-9a31-c01831687683

Supposed to be a "known" issue.

A: 

I found a fix here:

http://blog.jtango.net/2009/05/08/using-a-grid-as-the-itemspanel-for-a-listbox-in-wpf-binding-errors/

You set the style using ItemContainerStyle on the ListBox.

Beth