Skip to content

Instantly share code, notes, and snippets.

@TobiasSekan
Last active November 28, 2024 15:20
Show Gist options
  • Save TobiasSekan/73a93c2dfea4a051ff72abb5218d6f8f to your computer and use it in GitHub Desktop.
Save TobiasSekan/73a93c2dfea4a051ff72abb5218d6f8f to your computer and use it in GitHub Desktop.
[WPF] Fix for -> System.Windows.Data Error: 4 -> HorizontalContentAlignment and VerticalContentAlignment

Fix for debug output errors

Add the follow lines to the file App.xaml in your WPF application

    <!--
        Global fix for
        
        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')
    -->
    
   <Application.Resources>
        <Style TargetType="ComboBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Left" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
        </Style>
    </Application.Resources>
@derekhearst
Copy link

That worked! Thank you

@TobiasSekan
Copy link
Author

@derekhearst I am glad that it helped you.

@chenjing1294
Copy link

Can you tell me why this error occurs? I used your method to fix the problem, but I don’t know the principle.

@TobiasSekan
Copy link
Author

@chenjing1294
Hello, its long time ago that I must use this, IMHO possible on

  • A empty source
  • Empty elements in a source
  • The parent control have no defined size (size not set)
  • Use of auto-size and the control can determinate the size

@mjanulaitis
Copy link

Thank you for the fix.

@anders-eriksson
Copy link

First a big thank you!
This error is strange it complains about a combobox with no name. All my comboboxes has names. All my object has names to make it easier to find where the error is...

@Keke71
Copy link

Keke71 commented Jul 21, 2023

I had the same problem with a ListBox and a (temporarily) empty source. For me it was enough to just assign the default style:

<ListBox ItemContainerStyle="{StaticResource {x:Type ListBoxItem}}" .../>

@YetAnotherCodeMaker
Copy link

Thanks, this is still a fix as of today (.NET 9). In my case, the exceptions occur when I change the SortDescriptions of a CollectionViewSource at runtime and that makes the application totally unresponsive (the weird thing is it does not happen when the collection is first loaded, only when its sort order is modified).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment