如何在WPF输出窗口中消除“信息”绑定 - 无法使用绑定检索值

3

我正在编写一个WPF应用程序,但是性能有些慢,因此我正在尝试解决这个问题。当我运行时,我会收到大约无数个这种类型的消息:

System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=(0); DataItem=null; target element is 'Control' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=ValidationErrorTemplate; DataItem=null; target element is 'Control' (Name=''); target property is 'Template' (type 'ControlTemplate')

如果我制作一个小的示例应用程序:
<Window x:Class="bindingerrors.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:bindingerrors="clr-namespace:bindingerrors" Title="MainWindow">
    <Window.Resources>
        <bindingerrors:Thinger x:Key="Thing" />               
    </Window.Resources>
    <StackPanel>
        <DataGrid x:Name="TheGrid" ItemsSource="{Binding Stuff, Source={StaticResource Thing}}" AutoGenerateColumns="False" HeadersVisibility="Column">
            <DataGrid.Columns>
                <DataGridTextColumn Header="!" Binding="{Binding A, FallbackValue=''}" />
                <DataGridTextColumn Header="#" Binding="{Binding I, FallbackValue=''}" />              
                </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

以下是对象定义:
public class Thinger
{
    public ObservableCollection<ARow> Stuff { get; private set; }

    public Thinger()
    {
        //Fill up some bogus data
        string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int i = 0;
        Stuff = new ObservableCollection<ARow>();
        foreach (var letter in letters)
        {
            Stuff.Add(new ARow(letter.ToString(),i));
            i++;
        }
    }
}

public class ARow
{
    public string A { get; private set; }
    public int I { get; set; }

    public ARow(string a, int i)
    {
        A = a;
        I = i;
    }
}

执行时,我遇到了大量绑定问题。由于许多WPF性能文章声称失败的绑定会严重影响性能,我怀疑这可能是我问题的根源之一。
发生了什么?如何消除这些失败的绑定?我提供的示例尽可能简单,同时仍然表现出问题的症状,但它应该可以正常工作,不是吗?如果有任何区别,我正在构建.NET 4.0。
编辑:如果您尝试构建示例代码,则可以抑制错误。在Visual Studio选项->调试->输出窗口->数据绑定中,查看其是否设置为“信息”。
我看过Getting many Binding "Information" in WPF output window,但里面没有关于如何处理它的信息。
谢谢

1
我已经复制了你的代码,但是我无法重现绑定错误... 当你使用上述代码时,你是否收到了这些错误? - PGallagher
@PGallagher:是的,当使用那个确切的代码时,它是从一个测试项目中复制出来的。我想我得在另一台闲置的机器上构建,看看是否与我的环境有关。我也会在随机边框控件中收到绑定消息。 - whatsisname
我读到了一个与直接绑定无关的问题,那就是向Observable Collection添加太快可能会触发绑定错误...所以,如果可能的话,您可以尝试在For Each循环中加入延迟,看看是否解决了这个问题? - PGallagher
@PGallagher:在循环中添加延迟并没有改善任何事情。我还尝试了在窗口初始化后使用循环将项目添加到集合中,并通过按钮触发,但绑定错误仍然存在,实际上每添加一个项目,就会出现一批新的错误。 - whatsisname
如果您方便的话,请进入Visual Studio选项->调试->输出窗口->数据绑定,并确认它被设置为“信息”。我认为默认设置是警告,因此您可能已经屏蔽了这些消息。谢谢。 - whatsisname
你说得很对!选择“信息”会显示你所提到的绑定错误....我以前没有尝试过更改那个选项(现在我会了!)... - PGallagher
2个回答

1

有几件事情可以修复,以消除一些错误。

  1. Stuff 有一个私有的 setter,所以你需要将 BindingMode 设置为 OneWay
  2. A 有一个私有的 setter,所以你需要将 BindingMode 设置为 OneWay
  3. I 有一个备用值,不是一个 int

然而,这些都与您发布的错误不匹配,仅供参考。


0

找到所有绑定问题将耗费时间,以下解决方案很简单。

只需在您的应用程序构造函数中输入以下代码行。

System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level     =System.Diagnostics.SourceLevels.Critical;

通过在构造函数中添加上述代码行,调试器将忽略绑定错误并提高性能。
希望这可以帮到你。
谢谢, 迈克。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接