How to get rid of annoying HorizontalContentAlignment binding warning?

前端 未结 4 2092
既然无缘
既然无缘 2020-12-31 03:47

I am working on a large WPF project and during debug my output window is filled with these annoying warnings:

System.Windows.Data Information: 10 : Ca

4条回答
  •  悲哀的现实
    2020-12-31 04:48

    I just want to mention I struggled with a similar problem for two days (mine was a "Windows Data Error 4" error, complaining about HorizontalContentAlignment and VerticalContentAlignment).

    The most common suggested solution (adding the Horizontal/VerticalContentAlignment Style to your element, or even to the App.xaml) does NOT always solve the problem.

    Eventually, I discovered something unique to my own situation - I hope it can be of help to someone: If you are using the FilterEventHandler, don't unsubscribe it before resubscribing!

    My old code kept on generating that "Data Error 4" message whenever I changed the Channel Filter (which calls 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;
        }
    }
    

    ...so I changed it to re-subscribe to the FilterEventHandler every time, and rather put the check for a null on Channel Filter in the event-handling method.

    // 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! No more errors :-)

提交回复
热议问题