3
votes

I faced one issue and I would really appreciate it if you could provide any advice for this.

Error message:

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 'TreeViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

I have a treeview control (C# WPF .NET 4.0), and several items are added into this treeview by using datatemplate in xaml or manually, and both uses data binding.

When new data structure is received, I need to clean all items in treeview and regenerate new one by treeview_Control.Items.Clear() and it seems working fine from GUI point of view, but when I see output window on Visual Studio it shows several error messages as like above.

I've tried to search for a solution and tried several methods but no luck yet. Someone just recommended to just ignore this error message, but I just really want to clear this up.

If you have any idea, please help me for this.

3
can you share your xaml and viewmodel/codebehind ?Nitin

3 Answers

3
votes

Sorry, This is a comment but I cant fit in the comment section

Can you try add a ItemContainerStyle on the TreeView and see if it fixes the error, we had the same issue in a ListView and this was the only workaround we could find to remove the error.

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
    </Style>
</TreeView.ItemContainerStyle>
0
votes

I had same problem.When I was removing items from treeview via ViewModel ,I was receiving same harmless error messages. I have defined style for TreeViewItem in UserControl.Resources. In that style are also setters for content alignments.

<Style TargetType="TreeViewItem" x:Key="myTreeViewItemStyle"
             BasedOn="{StaticResource {x:Type TreeViewItem}}">
            <Setter Property="HorizontalContentAlignment"
                    Value="Left" />
            <Setter Property="VerticalContentAlignment" Value="Center"/>
    ...</Style>

What did help to me was addint ItemContainerStyle referenc in TreeView.

<TreeView ItemsSource="{Binding Items}" ItemContainerStyle="{StaticResource ResourceKey=myTreeViewItemStyle}">
0
votes

I finally found a solution that works for me. I first added a global style to my App.xaml:

<Style TargetType="{x:Type TreeViewItem}">
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
</Style>

I also had to add the following to my TreeView:

<TreeView.ItemContainerStyle>
    <Style
        BasedOn="{StaticResource {x:Type TreeViewItem}}"
        TargetType="{x:Type TreeViewItem}">         
    </Style>
</TreeView.ItemContainerStyle>

For some reason, the BasedOn attribute was the missing piece in my case.