首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ComboBoxItem继续抛出绑定错误,尽管样式

ComboBoxItem继续抛出绑定错误,尽管样式
EN

Stack Overflow用户
提问于 2013-02-25 15:51:36
回答 4查看 5.8K关注 0票数 6

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

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

代码语言:javascript
复制
    <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的方式符合他们的意图。

EN

回答 4

Stack Overflow用户

发布于 2015-12-03 20:02:38

我原以为我已经在自己的程序中解决了这个问题,但发现它一直断断续续地出现。终于找到了问题的根源。

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

重写它,使您只对给定事件调用一次Refresh(),将防止出现绑定错误。

票数 7
EN

Stack Overflow用户

发布于 2013-10-22 08:30:01

我只想提一提我为这个问题挣扎了两天。最常见的建议解决方案(将水平/VerticalContentAlignment样式添加到元素中,甚至添加到App.xaml中)并不总是解决问题。

最后,我发现了一些对我自己的情况独特的东西--我希望它能对某些人有所帮助:,如果你在使用FilterEventHandler,不要在重新订阅之前取消它!

每当我更改通道过滤器(调用UpdateCorporatesList)时,我的旧代码就会继续生成“数据错误4”消息:

代码语言:javascript
复制
// 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。

代码语言:javascript
复制
// 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;
    }
}

艾特·沃拉!不再有错误:-)

票数 3
EN

Stack Overflow用户

发布于 2021-05-25 20:41:12

与这个错误斗争了几个小时,尝试了谷歌的每一个解决方案,只有这个有效,从您的组合框样式中删除OverridesDefaultStyle属性行:

代码语言:javascript
复制
// 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内的组合框

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15070861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档