我有一个通过CollectionViewSource填充的组合框。这些项是通过输入项类型(在本例中是ProjectViewModel)的数据板构建的。这是在.NET 4.0中的WPF中。
在我的window.resources中,我指定了以下内容:
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>尽管有这种风格,但我仍然会遇到以下错误:
AncestorType='System.Windows.Controls.ItemsControl',System.Windows.Data错误:4:无法找到引用'RelativeSource FindAncestor,RelativeSource AncestorLevel=‘1’绑定的源代码。BindingExpression:Path=HorizontalContentAlignment;DataItem=null;目标元素是'ComboBoxItem‘(Name='');目标属性是'HorizontalContentAlignment’(键入'HorizontalAlignment') System.Windows.Data错误:4:无法找到与引用'RelativeSource FindAncestor,AncestorType='System.Windows.Controls.ItemsControl',AncestorLevel=‘1’绑定的源。BindingExpression:Path=VerticalContentAlignment;DataItem=null;目标元素是'ComboBoxItem‘(Name='');目标属性是'VerticalContentAlignment’(键入'VerticalAlignment')
我也在ComboBox元素上指定了水平和垂直的ComboBox,但没有效果。这不是一个可怕的问题,因为项目显示正确。但是,在调试时,当窗口关闭时,我确实会延迟大约10秒,同时它会将大约4000条错误消息输出到输出窗口(需要打开该窗口才能捕获合法的绑定错误)。
我可能看错了错误。为什么它找不到一个有效的绑定源?据我所知,我使用ComboBox和CollectionViewSource的方式符合他们的意图。
发布于 2015-12-03 20:02:38
我原以为我已经在自己的程序中解决了这个问题,但发现它一直断断续续地出现。终于找到了问题的根源。
如果您使用的是一个由ICollectionView支持的组合框,并且在事件队列上堆叠了两个或多个collectionView.Refresh()调用(例如:由于两个不同的清理操作而调用两次刷新),这将导致它在组合框的每个元素上生成绑定错误垃圾邮件。此绑定错误仅在您打开组合框至少一次之后才会发生。
重写它,使您只对给定事件调用一次Refresh(),将防止出现绑定错误。
发布于 2013-10-22 08:30:01
我只想提一提我为这个问题挣扎了两天。最常见的建议解决方案(将水平/VerticalContentAlignment样式添加到元素中,甚至添加到App.xaml中)并不总是解决问题。
最后,我发现了一些对我自己的情况独特的东西--我希望它能对某些人有所帮助:,如果你在使用FilterEventHandler,不要在重新订阅之前取消它!
每当我更改通道过滤器(调用UpdateCorporatesList)时,我的旧代码就会继续生成“数据错误4”消息:
// This code generates errors
private void UpdateCorporatesList()
{
this.CorporatesViewSource.Filter -= new FilterEventHandler(ApplyCorporateFilter);
if (this.ChannelFilter != null)
{
this.CorporatesViewSource.Filter += new FilterEventHandler(ApplyCorporateFilter);
}
else
{
this.CorporateFilter = null;
}
}
private void ApplyCorporateFilter(object sender, FilterEventArgs e)
{
SalesCorporate customer = e.Item as SalesCorporate;
var currentChannel = this.Channels.FirstOrDefault(x => x.ID == this.ChannelFilter).Description;
if ((customer.ID != null) && (customer.Channel != currentChannel))
{
e.Accepted = false;
}
}...so我每次都将它更改为重新订阅FilterEventHandler,而是在事件处理方法中在通道过滤器上检查null。
// This code works as intended
private void UpdateCorporatesList()
{
this.CorporatesViewSource.Filter += new FilterEventHandler(ApplyCorporateFilter);
if (this.ChannelFilter == null)
{
this.CorporateFilter = null;
}
}
private void ApplyCorporateFilter(object sender, FilterEventArgs e)
{
var currentChannel = this.Channels.FirstOrDefault(x => x.ID == this.ChannelFilter);
if (currentChannel.ID == null)
{
return;
}
SalesCorporate customer = e.Item as SalesCorporate;
if ((customer.ID != null) && (customer.Channel != currentChannel.Description))
{
e.Accepted = false;
}
}艾特·沃拉!不再有错误:-)
发布于 2021-05-25 20:41:12
与这个错误斗争了几个小时,尝试了谷歌的每一个解决方案,只有这个有效,从您的组合框样式中删除OverridesDefaultStyle属性行:
// before
<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
// after
<Style x:Key="{x:Type ComboBoxItem}" TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true" />用样式模板实现数据单元https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/combobox-styles-and-templates?view=netframeworkdesktop-4.8内的组合框
https://stackoverflow.com/questions/15070861
复制相似问题