2

I have a couple of comboboxes in a datatemple of an itemscontrol.The control works fine but when I click on the combobox and show the options I get the following error in the debugger output.

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')

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')

I looked at similar problems and adding the following xaml to control seemed to have fixed the issue for some people.

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

but I am still getting the same error. Any ideas what else could I change?

Here is my complete xaml for the control

     <ItemsControl ItemsSource="{Binding AccessControl.Credentials}" HorizontalContentAlignment="Stretch">                           
                            <ItemsControl.ItemTemplate>
                        <DataTemplate>
                                <Grid MinWidth="600" HorizontalAlignment="left" Margin="0,0,0,10">
                                <Grid.ColumnDefinitions >
                                    <ColumnDefinition Width="auto" />
                                        <ColumnDefinition Width="auto"/>
                                        <ColumnDefinition Width="auto"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>

                                    <Label  Grid.Column="0" 
                                            Content="{Binding Path=DisplayName}" 
                                            Width="200" 
                                            Margin="3,3,0,0"/>
     <ComboBox Grid.Column="2" 
               Grid.Row="0"  
               Name="chkFieldType"  
               Tag="{Binding Path=ID}" 
               Width="160"   
               Margin="19,0,0,0"                                               
               SelectionChanged="chkFieldType_SelectionChanged"         
               DropDownOpened="ComboBox_DropDownOpened"
               ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl},
                                                                                   Path=DataContext.AccessControl.TypeOptions}"
               SelectedValue="{Binding ValueSourceForViewModel,Mode=TwoWay}">
                     <ComboBox.Resources>
                           <Style TargetType="{x:Type ComboBox}">
                                <Style.Triggers>
                                     <DataTrigger Binding="{Binding Path=IsChecked, ElementName=chkPool}" Value="false">
                                          <Setter Property="Visibility" Value="Visible" />                                                           
                                     </DataTrigger>                                                      
                                </Style.Triggers>
                                <Setter Property="SnapsToDevicePixels" Value="true"/>
                                <Setter Property="Foreground" Value="#FF000000"/>
                                <Setter Property="Template" Value="{DynamicResource ComboBoxTemplate}" />
                          </Style>
                          <Style TargetType="{x:Type ComboBoxItem}">
                                <Setter Property="HorizontalContentAlignment" Value="Left" />
                                <Setter Property="VerticalContentAlignment" Value="Top" />
                          </Style>
                </ComboBox.Resources>
akjoshi
  • 15,374
  • 13
  • 103
  • 121
Marcom
  • 4,621
  • 8
  • 54
  • 78
  • You have bound ComboBox to some ItemsControl control, does it exist? Of which type? – sll Oct 25 '11 at 11:48
  • Try out to put along with ComboBox next : `` and see whether binding at least for datacontext is valid, perhaps FindAncestor does not foudn anything – sll Oct 25 '11 at 13:32
  • Have you checked this http://stackoverflow.com/questions/160391/listbox-with-grid-as-itemspaneltemplate-produces-weird-binding-errors ? – MichaelS Oct 27 '11 at 20:48
  • yes, that's the first thing I tried. Are you referring to a specific part of that question? – Marcom Nov 01 '11 at 14:59

1 Answers1

0

You should set the ItemContainerStyle of ComboBox instead of ComboBoxItem like this -

<ComboBox
    Name="control">
    <ComboBox.ItemContainerStyle>
        <Style
            TargetType="ComboBoxItem">
            <Setter
                Property="HorizontalContentAlignment"
                Value="Left" />
            <Setter
                Property="VerticalContentAlignment"
                Value="Center" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

This works but it’s very hard task to set these properties across the whole solution for each problamatic control(i.e. ListBox, Menu, ContextMenus etc.).

See my answer here for another one line workaround, which I find more appropriate and works for other such controls(like Menus, ListBox etc.) -

ListBox with Grid as ItemsPanelTemplate produces weird binding errors

Community
  • 1
  • 1
akjoshi
  • 15,374
  • 13
  • 103
  • 121
  • blogged about this here - http://weblogs.asp.net/akjoshi/archive/2011/11/30/resolving-un-harmful-binding-errors-in-wpf.aspx – akjoshi Dec 01 '11 at 10:17