尽管有样式,ComboBoxItem仍会引发绑定错误 [英] ComboBoxItem continues to throw binding error despite style

查看:74
本文介绍了尽管有样式,ComboBoxItem仍会引发绑定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过CollectionViewSource填充的组合框。通过传入项目类型的数据模板(在本例中为ProjectViewModel)构建项目。这是.NET 4.0中的WPF。

I have a combobox that I am populating via a CollectionViewSource. The items are build though a datatemplate for the incoming item type (in this case a ProjectViewModel). This is in WPF in .NET 4.0.

在我的window.resources中,我指定了以下内容:

In my window.resources, I have specified the following:

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

尽管这种样式,我仍然遇到以下错误:

Despite this style, I am still getting the following errors:


System.Windows.Data错误:4:找不到与
参考'RelativeSource FindAncestor,
AncestorType ='System.Windows.Controls.ItemsControl',
AncestorLevel绑定的源='1''。 BindingExpression:Path = Horizo​​ntalContentAlignment;
DataItem = null;目标元素是 ComboBoxItem(名称=);目标
属性为'Horizo​​ntalContentAlignment'(类型为'Horizo​​ntalAlignment')

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错误:4 :找不到带有
参考'RelativeSource FindAncestor,
AncestorType ='System.Windows.Controls.ItemsControl',
AncestorLevel ='1'的绑定源。 BindingExpression:Path = VerticalContentAlignment;
DataItem = null;目标元素是 ComboBoxItem(名称=);
的目标属性是'VerticalContentAlignment'(类型为'VerticalAlignment')

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

我也已在ComboBox元素上指定了Horizo​​ntal和Vertical ContentAlignment,无济于事。这不是一个严重的问题,因为项目正确显示。但是,在调试时,关闭窗口时确实会延迟10秒钟,同时它会向输出窗口输出大约4000条错误消息(我需要打开它才能捕获合法的绑定错误。

I have specified the Horizontal and Vertical ContentAlignment on the ComboBox element as well, to no avail. This is not a terrible problem as the items appear correctly. however when debugging, I do get about a 10 second delay when closing the window while it outputs about 4000 error messages to the output window (which I need open to catch legitimate binding errors.

I
可能无法正确读取该错误,为什么它找不到绑定的有效来源?据我所知,我使用ComboBox和CollectionViewSource的方式与他们的意图一致。

I may not be reading the error correctly. Why can it not find a valid source for the binding? As far as I know, the way I am using the ComboBox and CollectionViewSource is in line with their intent.

推荐答案

我以为我已经在自己的程序中解决了这个问题,但是发现它不断间歇地弹出,最终设法找到了源代码。

I'd thought I'd solved this problem in my own program, but found that it kept popping up intermittently. Finally managed to track down the source of the issue.

如果您使用的是由 ICollectionView 支持的组合框,并且您堆叠了两个或多个 collectionView.Refresh()对事件队列的调用(例如:由于两次不同的清除操作而调用两次刷新),这将导致它在每个队列上生成绑定错误垃圾邮件每个其他刷新的组合框元素()调用。仅在您至少打开一次组合框之后,才会发生此绑定错误。

If you're using a combobox backed by an ICollectionView, and you stack two or more collectionView.Refresh() calls on the event queue (ie: calling refresh twice because of two different cleanup operations, for example), that will cause it to generate the binding error spam on each element of the combobox for each additional Refresh() call made. This binding error will only occur after you have opened the combobox at least once.

重写它,以便仅调用 Refresh()对于给定事件一次将防止弹出绑定错误。

Rewriting it so that you only call Refresh() once for a given event will prevent the binding error from popping up.

这篇关于尽管有样式,ComboBoxItem仍会引发绑定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆