我正在编写一个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构建,如果这有任何区别的话。
编辑:如果您尝试构建示例代码,则可能会阻止错误。在视觉工作室选项中 - &gt;调试 - &gt;输出窗口 - &gt;数据绑定,看它是在“信息”上设置的。
我见过Getting many Binding "Information" in WPF output window,但没有关于该怎么做的信息。
由于
答案 0 :(得分:1)
你可以解决一些问题,以消除其中的一些错误。
Stuff
有一个私人的二传手,所以你必须设置
BindingMode
至OneWay
A
有一个私人的二传手,所以你必须设置
BindingMode
至OneWay
I
的后备值不是int
值然而,这些都不符合您发布的错误,只是一些观察
答案 1 :(得分:0)
查找所有绑定问题将非常耗时,以下解决方案很简单。
只需在应用程序构造函数中输入以下代码行。
System.Diagnostics.PresentationTraceSources.DataBindingSource.Switch.Level =System.Diagnostics.SourceLevels.Critical;
通过在构造函数中添加上面的行,调试器将忽略绑定错误,并且性能得到改善。
希望这有帮助。
谢谢, 麦克