美好的一天,ComboBoxItem继续抛出绑定错误,尽管风格
我有一个combobox,我通过一个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 : 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')
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 'ComboBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')
我指定的水平和垂直的ContentAlignment在ComboBox元素上,也无济于事。这并不是一个可怕的问题,因为这些项目显示正确。然而,在调试时,当关闭窗口时,我确实得到大约10秒的延迟,同时它向输出窗口输出大约4000条错误消息(我需要打开这个窗口才能捕获合法的绑定错误。)
我可能没有正确读取错误。为什么找不到绑定的有效源?据我所知,我使用ComboBox和CollectionViewSource的方式与他们的意图一致。
我认为有人在这里解决这个问题:http://stackoverflow.com/questions/2666439/how-to-get-rid-of-annoying-horizontalcontentalignment-binding-warning – 2013-02-25 15:57:21
@DJBurb在这个问题的两个建议基本上与我在解决方案中的风格相同。我曾尝试在App.xaml中级别的风格,我试图将其命名为类型名ASLO。没有变化。令人奇怪的是在进行在圆K. – CodeWarrior 2013-02-25 16:03:42