2013-02-25 73 views
2

美好的一天,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的方式与他们的意图一致。

+0

我认为有人在这里解决这个问题:http://stackoverflow.com/questions/2666439/how-to-get-rid-of-annoying-horizo​​ntalcontentalignment-binding-warning – 2013-02-25 15:57:21

+0

@DJBurb在这个问题的两个建议基本上与我在解决方案中的风格相同。我曾尝试在App.xaml中级别的风格,我试图将其命名为类型名ASLO。没有变化。令人奇怪的是在进行在圆K. – CodeWarrior 2013-02-25 16:03:42

回答

3

我只想提一提我为这个问题而奋斗最常见的建议解决方案(向您的元素或甚至App.xaml添加Horizo​​ntal/VerticalContentAlignment样式)并不总能解决问题。 lly,我发现了一些与我自己的情况相似的东西 - 我希望它对某人有所帮助:如果您使用的是FilterEventHandler,请勿在重新订阅前取消订阅!

我以前的代码保存在生成​​“数据错误4”的消息时,我改变信道滤波器(它调用UpdateCorporatesList):

// 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; 
    } 
} 

...所以我改成了重新订阅FilterEventHandler,而是在事件处理方法的Channel Filter中检查一个空值。

// 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; 
    } 
} 

Et Voila!没有更多的错误:-)

0

我不知道你是否仍然需要帮助,但我只是想出了一种方法来使这个错误/警告disapear。 在我的组合框,我重新定义了ItemTemplate属性是这样的:

<ComboBox.ItemTemplate> 
    <ItemContainerTemplate> 
     <TextBlock Text="{Binding Path=YourBinding}"/> 
    </ItemContainerTemplate> 
</ComboBox.ItemTemplate> 

YourBinding是你的“的DisplayMemberPath”使用组合框的值

+0

我早已完成了这个项目,并且不容易访问源。我想* *我有一个自定义的ItemTemplate,但我不知道。不幸的是,就像是个月(如果有的话)之前,我回到这个项目再次可以检查。 – CodeWarrior 2014-01-14 19:42:14

1

我还以为我已经解决了这个问题我自己的节目,但发现它不断弹出。最后设法追查问题的根源。

如果您使用由ICollectionView支持的组合框,你栈上的事件队列两个或两个以上collectionView.Refresh()电话(即:调用刷新两次,因为两个不同的清理操作,例如),这将导致其在每个附加的Refresh()调用中,在组合框的每个元素上生成绑定错误垃圾邮件。此绑定错误只会在您打开组合框至少一次后才会发生。

重写,这样你只能叫Refresh()一次给定的事件会阻止绑定错误从弹出。