none
Binding Error When Using CompositeCollection for MenuItems

    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')
    
      <Menu.Resources>
       <Style x:Key="ItemStyle">
        <Setter Property="MenuItem.Header" Value="{Binding DisplayName}" />
        <Setter Property="MenuItem.Command" Value="{Binding Command}" />
       </Style>
       <CollectionViewSource x:Key="CVS" Source="{Binding Commands}">
        <CollectionViewSource.GroupDescriptions>
         <PropertyGroupDescription PropertyName="Category"/>
        </CollectionViewSource.GroupDescriptions>
       </CollectionViewSource>
       <HierarchicalDataTemplate DataType="{x:Type CollectionViewGroup}" ItemsSource="{Binding Items}" ItemContainerStyle="{StaticResource ItemStyle}">
        <ContentPresenter Content="{Binding Path=Name}" RecognizesAccessKey="True" />
       </HierarchicalDataTemplate>
      </Menu.Resources>
      <Menu.ItemsSource>
       <CompositeCollection>
        <MenuItem Header="_File">
         <MenuItem Header="E_xit" Command="{Binding Path=CloseCommand}" />
        </MenuItem>
        <MenuItem x:Name="ViewMenu" Header="_View">
         <MenuItem.ItemsSource>
          <CompositeCollection>
           <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}">
            <MenuItem.Style>
             <Style TargetType="{x:Type MenuItem}">
              <Style.Triggers>
               <DataTrigger Binding="{Binding ToolBarVisibility, Mode=OneWay}" Value="Visible">
                <Setter Property="IsCheckable" Value="True" />
               </DataTrigger>
               <DataTrigger Binding="{Binding ToolBarVisibility, Mode=OneWay}" Value="Hidden">
                <Setter Property="IsCheckable" Value="False" />
               </DataTrigger>
              </Style.Triggers>
             </Style>
            </MenuItem.Style>
           </MenuItem>
           <MenuItem Header="_Status Bar" IsCheckable="True" IsChecked="True" Command="{Binding StatusBarCommand}" />
           <Separator />
           <CollectionContainer Collection="{Binding Source={StaticResource CVS}, Path=Groups}" />
          </CompositeCollection>
         </MenuItem.ItemsSource>
        </MenuItem>
        <CollectionContainer Collection="{Binding Source={StaticResource CVS}, Path=Groups}" />
        <MenuItem Header="_Help">
         <MenuItem Header="_About" />
        </MenuItem>
       </CompositeCollection>
      </Menu.ItemsSource>
     </Menu>
    Any ideas how I can resolve these?
    Thanks.
    • Edited by mschmit Tuesday, October 5, 2010 7:47 PM Formatting
    Tuesday, October 5, 2010 7:45 PM

Answers

  • 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"/>
          </Style>
    

    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,

    Min


    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
    Moderator