在调试我的WPF项目时,我看到在输出窗口中记录了很多绑定错误,如下所示:
System.Windows.Data信息:10:无法使用绑定检索值,并且不存在有效的回退值;请改用默认值。bindingExpression:path=arerowdetailsFrozed;dataitem=null;目标元素是'dataGridDetailsPresenter'(name='');目标属性是'selectiveScrollingOrientation'(类型'selectiveScrollingOrientation')
我在网上搜索了很多关于这类信息的信息,并试图解决所有问题。
我的
绑定,但对于我从未听说过的属性,错误会不断发生。
所以我把它分解成一个基本的例子:
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')
我检查了设置时出现的错误
项目源
返回
Items
,设置为时不显示
null
. 错误消息的数量取决于集合中的项数。
我担心这些“绑定错误”会减慢我的实际应用程序的速度(我可能在集合中有50k个元素),因此
想知道他们为什么出现以及我如何避免他们
.
正如您所看到的,我已经向绑定中添加了回退值,但是对于我根本没有绑定的属性,错误仍然会出现。