我有一个包含DataGrid
的WPF应用程序,如下所示:
数据网格(* 简化 *):
<DataGrid x:Name="CoreServiceLogDataGrid"
Grid.Row="0"
Height="auto"
ItemsSource="{Binding Source={StaticResource CoreServiceCollection}}"
AutoGenerateColumns="False"
CanUserReorderColumns="True"
CanUserSortColumns="True"
IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn x:Name="ID"
Header="ID"
Binding="{Binding ID}" />
<DataGridTextColumn Binding="{Binding Timestamp}"
Header="Timestamp" />
</DataGrid.Columns>
</DataGrid>
数据加载时;我收到以下错误(多次):
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')
我不知道为什么会发生这种情况,以及如何解决这个问题。
编辑:(有关核心服务日志视图集合的信息)
核心服务集合只是一个列表集合视图。
public static ListCollectionView CoreServiceLogViewCollection {
get {
if (_coreServiceCollection == null) {
_coreServiceCollection =
new ListCollectionView(LogSession.CoreServiceLogCollection);
}
return _coreServiceCollection;
}
}
参数只是一个包含ID
、Timestamp
和其他属性的ObservableCollection
EDIt 2:示例化在App.xaml中完成:
<ResourceDictionary>
<x:Static Member="vm2:CoreServiceLogView.CoreServiceLogViewCollection"
x:Key="CoreServiceCollection" />
</ResourceDictionary>
编辑3(风格...)
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<!-- #columnHeaderDGStyle -->
<!-- Datagrid -->
<Style x:Key="Log4NetDataGridStyle"
TargetType="DataGrid">
<Setter Property="ColumnHeaderStyle"
Value="{DynamicResource DatagridColumnHeaderCustomTemplateStyle}" />
<Setter Property="RowStyle"
Value="{DynamicResource Log4NetRowStyle}" />
<Setter Property="RowDetailsTemplate"
Value="{DynamicResource RowDetailsTemplate}" />
<Setter Property="MaxHeight"
Value="1600">
</Setter>
<Setter Property="MaxWidth"
Value="2560">
</Setter>
</Style>
<Style x:Key="DataCommuGridStyle"
TargetType="DataGrid">
<Setter Property="ColumnHeaderStyle"
Value="{DynamicResource DatagridColumnHeaderCustomTemplateStyle}" />
<Setter Property="RowStyle"
Value="{DynamicResource CommuRowStyle}" />
<Setter Property="RowDetailsTemplate"
Value="{DynamicResource RowDetailsTemplate}" />
<Setter Property="MaxHeight"
Value="1600">
</Setter>
<Setter Property="MaxWidth"
Value="2560">
</Setter>
</Style>
<!-- ************************* Row Style ************************* -->
<Style x:Key="Log4NetRowStyle"
TargetType="DataGridRow">
<Setter Property="FontSize"
Value="14" />
<Setter Property="Background"
Value="{Binding Path=LogColour.ColorName}" />
<Setter Property="Height"
Value="Auto">
</Setter>
<Style.Triggers>
<DataTrigger></DataTrigger>
</Style.Triggers>
</Style>
<Style x:Key="CommuRowStyle"
TargetType="DataGridRow">
<Setter Property="FontSize"
Value="14" />
<Setter Property="Background"
Value="Azure" />
<Setter Property="Height"
Value="Auto">
</Setter>
<Style.Triggers>
<DataTrigger></DataTrigger>
</Style.Triggers>
</Style>
<DataTemplate x:Key="RowDetailsTemplate">
<Border BorderThickness="0"
Padding="5" >
<Border.Background>
<LinearGradientBrush StartPoint="0,0"
EndPoint="0,1" Opacity="0.2">
<GradientStop Color="White"
Offset="0" />
<GradientStop Color="Black"
Offset="1" />
</LinearGradientBrush>
</Border.Background>
<!-- alternative with Expancer -->
<Expander IsExpanded="True"
HorizontalAlignment="Left"
BorderThickness="1,1,1,5"
MaxHeight="300"
MaxWidth="900">
<Expander.Header>
<DockPanel>
<TextBlock FontSize="12"
Text="LoggingMessage: "
VerticalAlignment="Center" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ScrollViewer VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
CanContentScroll="True"
Style="{StaticResource LeftScrollViewer}">
<StackPanel Orientation="Vertical">
<TextBox FontSize="16"
BorderThickness="0"
IsReadOnly="True"
Background="Transparent"
Foreground="Black"
TextWrapping="Wrap"
Text="{Binding LoggingMessage, Mode=OneWay}" />
</StackPanel>
</ScrollViewer>
</Expander.Content>
</Expander>
</Border>
</DataTemplate>
<Style x:Key="GroupHeaderStyle"
TargetType="{x:Type GroupItem}">
<Setter Property="Margin"
Value="0,0,0,5" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="False"
Background="#FF112255"
BorderBrush="#FF002255"
Foreground="Black"
BorderThickness="1,1,1,5">
<Expander.Header>
<DockPanel>
<TextBlock FontWeight="Bold"
Foreground="White"
Text="{Binding Path=Name}"
Margin="5,0,0,0"
Width="100" />
<TextBlock FontWeight="Bold"
Foreground="White"
Text="{Binding Path=ItemCount}" />
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- ******************** DataTemplate ******************** -->
</ResourceDictionary>
4条答案
按热度按时间oprakyz71#
不久前我写了一个关于如何阅读WPF绑定错误的答案。基本上,将错误分解到分号上,然后从下往上阅读,它应该会给予您知道绑定错误在哪里:
和
从下往上阅读,第一个错误是告诉你
SelectiveScrollingOrientation
DataGridDetailsPresenter
,未指定名称DataContext
是null
DataGrid
类型的RelativeSource
,以便它可以绑定到AreRowDetailsFrozen
属性,但未能找到该RelativeSource
因此,请仔细查看您的代码,看看是否有类似于以下内容的内容:
第二个错误是告诉你
Visibility
DataGridRowHeader
,未指定名称DataContext
是null
DataGrid
类型的RelativeSource
,以便它可以绑定到HeadersVisibility
属性,但未能找到该RelativeSource
因此,请仔细查看您的代码,看看是否有类似于以下内容的内容:
根据您发布的代码,第一个可能在您的
LeftScrollViewer
风格中,第二个可能在您的DatagridColumnHeaderCustomTemplateStyle
中如果在XAML中查找错误时遇到问题,可以尝试运行应用程序并使用Snoop之类的工具检查它,该工具允许您在WPF应用程序运行时查看其VisualTree,您应该能够在其中找到确切的绑定错误,以便可以在XAML中追溯到错误源
acruukt92#
此错误发生在DataGridRow的ControlTemplate中,DataGridRow包含具有上述绑定的DataGridRowHeader和DataGridDetailsPresenter。我在.NET 4.5 DataGrid中遇到了相同的问题。似乎只有在DataGrid对其项使用虚拟化时才会发生这两个错误。您可以尝试在DataGrid中禁用它。在我看来,当DataGridRow以某种方式与DataGrid附加/分离时,就会发生这种情况'的DataGrid,然后绑定会丢失或仍然找不到DataGrid的相对绑定目标。
hgtggwj03#
我有一个数据网格,其中有嵌套的数据网格作为行详细信息。我也寻找了很长时间的正确答案来解决这个问题。
DataGridRow模板出现问题。只需查找“DataGrid样式和模板”-并打开Microsoft站点。
如果您要在那里寻找DataGridRow模板(文字):DataGridRow的样式和模板。
您会在其中找到以下代码:
而问题就在这里。
在寻找解决方案的过程中,我发现了以下建议-以这种方式更改DataGridRow的样式:
它工作了,但是我的行详细信息不见了。在我添加了原始模板中缺少的部分之后:
我又犯了这个错误。所以我们的目标是改变这个部分:
改为定值,这样:
或者,您可以命名DataGrid并执行以下操作:
请注意,我已经删除了RowHeaders部分,因为我不使用它。
cngwdvgl4#
对于在数据网格上使用筛选的任何人:
我在数据网格上使用GroupStyle,必须添加
在添加已筛选的项目之前。