我有一个下拉框,我通过CollectionViewSource填充它。 这些项是通过数据模板为传入的项目类型(在本例中为ProjectViewModel)构建的。这是在.NET 4.0中的WPF。
在我的window.resources中,我指定了以下内容:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
尽管我按照这种样式,但仍然收到以下错误提示:
System.Windows.Data Error: 4 : 找不到绑定的源,参考'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')
System.Windows.Data Error: 4 : 找不到绑定的源,参考'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')
我在ComboBox元素上指定了水平和垂直ContentAlignment,但是没有效果。虽然项目显示正确,但调试时关闭窗口时会有大约10秒的延迟,同时输出大约4000个错误消息到输出窗口(我需要打开以捕获合法的绑定错误)。
我可能没有正确地阅读错误。为什么它找不到绑定的有效源?就我所知,我使用ComboBox和CollectionViewSource的方式符合它们的意图。