组合框抛出数据绑定错误

时间:2011-10-25 09:05:57

标签: .net wpf xaml data-binding combobox

我在itemscontrol的数据表中有几个组合框。控件工作正常但是当我点击组合框并显示选项时,我在调试器输出中得到以下错误。

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

我查看了类似的问题,并添加了以下xaml来控制似乎已经解决了一些人的问题。

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

但我仍然遇到同样的错误。任何想法我还能改变什么?

这是我完整的控件xaml

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

1 个答案:

答案 0 :(得分:0)

你应该像这样设置ComboBox的ItemContainerStyle而不是ComboBoxItem -

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

这是有效的,但是为每个problamatic控件(即ListBox,Menu,ContextMenus等)在整个解决方案中设置这些属性是非常困难的。

请在此处查看我的答案,了解另一种解决方法,我发现它更适合其他此类控件(如菜单,ListBox等) -

ListBox with Grid as ItemsPanelTemplate produces weird binding errors