Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot find source for binding with reference... databound ListView problem
    text
    copied!<p>I know that there have been questions regarding this error, I've found some and read them but to be honest, I didn't understand a thing.</p> <p>I have a WPF window with two databound ListViews. One is bound to business objects (my custom classes), another to a <code>Dictionary&lt;string, string&gt;</code>. Everything seems to look OK in runtime, but I'm getting errors in the Output window:</p> <p><code>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')</code></p> <p>and the same for <code>VerticalContentAlignment</code>.</p> <p>Even though bost ListViews get populated with items as expected, it actually causes a noticeable delay when loading the window.</p> <p>Looking for an answer, I found this thread <a href="http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768" rel="noreferrer">http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768</a> - and I implemented the suggested solution, supplying default values of both <code>HorizontalContentAlignment</code> and <code>VerticalContentAlignment</code> in both ListViews. It didn't help.</p> <p>Here's the XAML:</p> <ul> <li><p>ListView 1:</p> <pre><code> &lt;ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="13" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"&gt; &lt;ListView.Resources&gt; &lt;Style TargetType="ListViewItem"&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt; &lt;Setter Property="VerticalContentAlignment" Value="Stretch" /&gt; &lt;/Style&gt; &lt;/ListView.Resources&gt; &lt;ListView.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;UniformGrid Columns="3" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListView&gt; </code></pre></li> <li><p>ListView 2</p> <p> </p> <pre><code> &lt;ListView.Resources&gt; &lt;Style TargetType="ListViewItem"&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt; &lt;Setter Property="VerticalContentAlignment" Value="Stretch" /&gt; &lt;EventSetter Event="Selected" Handler="lvItemSelected" /&gt; &lt;/Style&gt; &lt;Style x:Key="GrayOutMappedColumn" TargetType="{x:Type TextBlock}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Mapped}" Value="False"&gt; &lt;Setter Property="TextElement.Foreground" Value="Black" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;Setter Property="TextElement.Foreground" Value="DarkGray" /&gt; &lt;/Style&gt; &lt;/ListView.Resources&gt; &lt;ListView.GroupStyle&gt; &lt;GroupStyle&gt; &lt;GroupStyle.HeaderTemplate&gt; &lt;DataTemplate&gt; &lt;Border BorderBrush="LightGray" BorderThickness="0,0,0,1"&gt; &lt;TextBlock FontSize="12" FontWeight="Bold" Margin="0,10" Text="{Binding Name}" /&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/GroupStyle.HeaderTemplate&gt; &lt;/GroupStyle&gt; &lt;/ListView.GroupStyle&gt; &lt;ListView.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;UniformGrid Columns="4" /&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ListView.ItemsPanel&gt; &lt;ListView.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;StackPanel ClipToBounds="False" HorizontalAlignment="Stretch" Width="Auto"&gt; &lt;TextBlock HorizontalAlignment="Stretch" Style="{StaticResource GrayOutMappedColumn}" Text="{Binding Path=FriendlyName}" Width="Auto" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/ListView.ItemTemplate&gt; </code></pre></li> </ul> <p>Data-binding code:</p> <pre><code>lvLanguageCodes.ItemsSource = languages; lvLanguageCodes.SelectedValuePath = "Key"; lvLanguageCodes.DisplayMemberPath = "Value"; </code></pre> <p>2:</p> <pre><code>lvDataTypes.ItemsSource = AssignDataType.datatypes; </code></pre> <p>where <code>datatypes</code> is <code>ObservableCollection&lt;Gate&gt;</code>, where <code>Gate</code> is my business class (implementing <code>INotifyPropertyChanged</code>, <code>IComparable</code> and nothing special about it otherwise).</p> <p>Why am I getting an error? Why is it trying to bind these alignment properties to anything at all, if I set their values explicitly?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload