Answered by:
WPF Binding Errors (System.Windows.Data Error 4)

Question
-
Hello,
I am experiencing a problem with a WPF ListView control. I get a lot of the following errors:
/////
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 'ListViewItem' (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 'ListViewItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
/////
I have a System.Windows.Controls.ListView control in my XAML file (say: file1.xaml):<ListView HorizontalContentAlignment="Left" VerticalContentAlignment="Top" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Name="lvSomething" Width="650" Height="320" />
And somewhere in a loop in the file1.xaml.cs file, I do this:
var col = new GridViewColumn(); var bnd = new Binding(string.Format("[{0}]", i)); bnd.Mode = BindingMode.OneWay; col.Header = someArrayWithHeaderStrings[i]; col.DisplayMemberBinding = bnd; gridView.Columns.Add(col);
And somewhere else I add items:
var lvi = new ListViewItem(); lvi.Content = new string[] { "a", "b" }; lvSomething.Items.Add(lvi);
So I have no idea what I should do in order to get rid of these errors.
Thanks in advance,
JFRWednesday, December 9, 2009 2:53 PM
Answers
-
This is a "known" issue, and happens to all user controls that contain dynamically created lists (all item controls, even comboboxes, menus, etc).
For more information, see this thread.
Summary: Ignore these binding errors, it's a timing issue in WPF itself.
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
Looking for a way to deploy your updates to all your clients? Try Updater!- Marked as answer by Linda Liu Friday, December 11, 2009 3:30 PM
Friday, December 11, 2009 10:40 AM
All replies
-
For me your code seems to be ok.
Do you bind HorizontalContentAlignment in a ControlTemplate?Wednesday, December 9, 2009 3:53 PM -
Well, I actually didn't...
So I made this:<ListView HorizontalContentAlignment="Left" VerticalContentAlignment="Top" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0" Name="lvSomething" Width="650" Height="320"> <Style TargetType="ListView"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListView"> <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Top"></ContentPresenter> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListView>
But now I get a whole different error (less than before, though):
System.Windows.Data Error: 39 : BindingExpression path error: '[]' property not found on 'object' ''Style' (HashCode=13)'. BindingExpression:Path=[0]; DataItem='Style' (HashCode=13); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
I get about 7 of these in comparison with something like 30 of the first errors I mentioned.
But still I can't seem to fix it!Thursday, December 10, 2009 7:26 AM -
This is a "known" issue, and happens to all user controls that contain dynamically created lists (all item controls, even comboboxes, menus, etc).
For more information, see this thread.
Summary: Ignore these binding errors, it's a timing issue in WPF itself.
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
Looking for a way to deploy your updates to all your clients? Try Updater!- Marked as answer by Linda Liu Friday, December 11, 2009 3:30 PM
Friday, December 11, 2009 10:40 AM -
Bedankt.
Ik kan leven met die foutmelding, maar het zorgt er wel voor dat m'n hele output window vol komt te staan...
:-)Friday, December 11, 2009 2:45 PM -
Volgende keer gewoon Engels gebruiken, dan snapt de rest er ook nog iets van ;)
Yes, that is the reason why we have developed a custom output window that files the non-interesting errors (and shows the errors in red, warnings in orange, etc).
Geert van Horrik - CatenaLogic
Visit my blog: http://blog.catenalogic.com
Looking for a way to deploy your updates to all your clients? Try Updater!Friday, December 11, 2009 3:22 PM