【发布时间】:2018-08-14 07:40:52
【问题描述】:
在调试我的 wpf 项目时,我看到在输出窗口中记录了很多绑定错误,如下所示:
System.Windows.Data 信息:10:无法使用绑定检索值,并且不存在有效的备用值;改为使用默认值。绑定表达式:路径=AreRowDetailsFrozen;数据项=空;目标元素是'DataGridDetailsPresenter'(名称='');目标属性是“SelectiveScrollingOrientation”(输入“SelectiveScrollingOrientation”)
我在 Google 上搜索了很多关于此类消息的信息,并尝试修复所有 我的 绑定,但我从未听说过的属性不断出现错误。
所以我将其分解为一个基本示例:
xaml:
<StackPanel>
<DataGrid ItemsSource="{Binding Items}" x:Name="_grid" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID, FallbackValue=0}"/>
<DataGridTextColumn Header="Text" Binding="{Binding Text, FallbackValue={x:Null}}"/>
</DataGrid.Columns>
</DataGrid>
<Button Click="Button_OnClick">Reset</Button>
</StackPanel>
后面的代码:
public partial class MainWindow
{
public ObservableCollection<TestItem> Items { get; } = new ObservableCollection<TestItem>();
public MainWindow()
{
Items.Add(new TestItem { ID = 1, Text = "One" });
Items.Add(new TestItem { ID = 2, Text = "Two" });
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
_grid.ItemsSource = null;
_grid.ItemsSource = Items;
}
}
public class TestItem
{
public int ID { get; set; }
public string Text { get; set; }
}
这两个元素在DataGrid中正确显示。
现在,每当我单击按钮(并重新分配 ItemSource)时,我都会在输出窗口中看到这 12 条消息:
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=ID; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Text; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
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')
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=ID; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Information: 10 : Cannot retrieve value using the binding and no valid fallback value exists; using default instead. BindingExpression:Path=Text; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
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')
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')
我检查了将ItemSource 设置回Items 时出现的错误,而不是设置为null 时出现的错误。并且错误消息的数量取决于集合中的项目数。
我担心这些“绑定错误”会减慢我的实际应用程序(我的集合中可能有 >50k 元素),因此想了解它们出现的原因以及如何避免它们.
如您所见,我已经在绑定中添加了备用值,但是对于我根本没有绑定的属性,错误仍然出现。
【问题讨论】:
-
我无法重现该问题。是 ID 和 Text - 属性吗?
-
@Rekshino 是的,它们是属性,我会将这个小类添加到问题中。要重现,您必须调试应用程序并观察输出窗口,也许您有不同的跟踪设置,以便不记录消息(跟踪设置描述为here)
-
如果我将跟踪级别设置为详细,那么我可以看到条目。问题是——你应该担心他们吗?
-
@Rekshino 似乎出现消息是因为行是在将它们插入可视化树之前设置的,所以最初它们的 datacontext 为空,它们仅在第二次尝试时才获得它们的值......我不确定我是否真的应该担心这一点,但到目前为止我所读到的内容表明这些错误确实会对性能产生影响(不确定它们是否都是内部昂贵的异常)。我仍在开发我的应用程序,所以我没有最终的性能数据,但已经感觉到我的数据网格变慢了。 (顺便说一句:感谢您花时间复制)。
标签: wpf data-binding datagrid wpfdatagrid