组合框抛出数据绑定错误
我在 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该像这样设置 ComboBox 的 ItemContainerStyle 而不是 ComboBoxItem -
这可行,但在整个解决方案中为每个问题控件(即 ListBox、Menu、ContextMenus 等)设置这些属性是一项非常艰巨的任务。
请参阅我的答案,了解另一种一行解决方法,我发现它更合适并且适用于其他此类控件(如菜单、列表框等) -
将网格作为 ItemsPanelTemplate 的列表框会产生奇怪的绑定错误
You should set the ItemContainerStyle of ComboBox instead of ComboBoxItem like this -
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