Binding Error When Using CompositeCollection for MenuItems RRS feed

  • Question

  • I'm a WPF rookie, so bare with me. I am receiving the following output errors when running my menu item 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 'MenuItem' (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 'MenuItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
       <Style x:Key="ItemStyle">
        <Setter Property="MenuItem.Header" Value="{Binding DisplayName}" />
        <Setter Property="MenuItem.Command" Value="{Binding Command}" />
       <CollectionViewSource x:Key="CVS" Source="{Binding Commands}">
         <PropertyGroupDescription PropertyName="Category"/>
       <HierarchicalDataTemplate DataType="{x:Type CollectionViewGroup}" ItemsSource="{Binding Items}" ItemContainerStyle="{StaticResource ItemStyle}">
        <ContentPresenter Content="{Binding Path=Name}" RecognizesAccessKey="True" />
        <MenuItem Header="_File">
         <MenuItem Header="E_xit" Command="{Binding Path=CloseCommand}" />
        <MenuItem x:Name="ViewMenu" Header="_View">
           <MenuItem Header="_Back" Command="{Binding BackCommand}" IsEnabled="{Binding IsBackEnabled, TargetNullValue=False, FallbackValue=False}" />
           <MenuItem Header="_Forward" Command="{Binding ForwardCommand}" IsEnabled="{Binding IsForwardEnabled, TargetNullValue=False, FallbackValue=False}" />
           <MenuItem Header="_Home" Command="{Binding HomeCommand}" />
           <Separator />
           <MenuItem Header="_Navigation Bar" IsChecked="True" IsCheckable="True" Command="{Binding NavBarCommand}" />
           <Separator />
           <MenuItem Header="_Toolbar" IsCheckable="True" Command="{Binding ToolBarCommand}">
             <Style TargetType="{x:Type MenuItem}">
               <DataTrigger Binding="{Binding ToolBarVisibility, Mode=OneWay}" Value="Visible">
                <Setter Property="IsCheckable" Value="True" />
               <DataTrigger Binding="{Binding ToolBarVisibility, Mode=OneWay}" Value="Hidden">
                <Setter Property="IsCheckable" Value="False" />
           <MenuItem Header="_Status Bar" IsCheckable="True" IsChecked="True" Command="{Binding StatusBarCommand}" />
           <Separator />
           <CollectionContainer Collection="{Binding Source={StaticResource CVS}, Path=Groups}" />
        <CollectionContainer Collection="{Binding Source={StaticResource CVS}, Path=Groups}" />
        <MenuItem Header="_Help">
         <MenuItem Header="_About" />
    Any ideas how I can resolve these?
    • Edited by mschmit Tuesday, October 5, 2010 7:47 PM Formatting
    Tuesday, October 5, 2010 7:45 PM


  • Hi Mschmit,

    This happens when you set MenuItem.ItemsSource to a Collection that contains MenuItems. This error has already been handled internally, so you can just leave it alone.

    Or you can explicit set MenuItem.HorizontalContentAlignment property and VerticalContentAlignment property to avoid this error. You can simply put the following code in application.Resource to achieve this.

          <Style TargetType="MenuItem">
           <Setter Property="HorizontalContentAlignment" Value="Left"/>
           <Setter Property="VerticalContentAlignment" Value="Center"/>

    You can also find a discussion of this issue at http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8028a46b-3080-40ad-aed2-e9417eb25381.

    Best regards,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by mschmit Friday, October 8, 2010 2:04 PM
    Friday, October 8, 2010 9:29 AM