gpt4 book ai didi

c# - WPF 数据网格 [System.Windows.Data 错误 : 4]

转载 作者:行者123 更新时间:2023-11-30 19:28:25 24 4
gpt4 key购买 nike

我有一个带有 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')

我不知道为什么会发生这种情况以及如何解决这个问题。

编辑:(有关 CoreServiceLogViewCollection 的信息)

CoreServiceCollection 只是一个 ListCollectionView。

  public static ListCollectionView CoreServiceLogViewCollection {
get {
if (_coreServiceCollection == null) {
_coreServiceCollection =
new ListCollectionView(LogSession.CoreServiceLogCollection);
}

return _coreServiceCollection;
}
}

参数只是一个ObservableCollection,包含IDTimestamp和其他属性

编辑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>

最佳答案

我前阵子写了一个关于how to read WPF binding errors的回答.基本上,将您的错误分解为分号并从下往上开始阅读,它应该让您了解绑定(bind)错误的位置:

  • System.Windows.Data 错误:4:
    • 找不到引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.DataGrid',AncestorLevel='1'”的绑定(bind)源。 BindingExpression:Path=AreRowDetailsFrozen;
  • DataItem=null;
  • 目标元素是 'DataGridDetailsPresenter' (Name='');
  • 目标属性是“SelectiveScrollingOrientation”(输入“SelectiveScrollingOrientation”)

  • System.Windows.Data 错误:4:
    • 找不到引用“RelativeSource FindAncestor,AncestorType='System.Windows.Controls.DataGrid',AncestorLevel='1'”的绑定(bind)源。 BindingExpression:Path=HeadersVisibility;
  • DataItem=null;
  • 目标元素是 'DataGridRowHeader' (Name='');
  • 目标属性是“Visibility”(输入“Visibility”)

自下而上阅读,第一个错误告诉你

  • 包含导致错误的绑定(bind)的属性是 SelectiveScrollingOrientation
  • 包含有问题属性的 UI 对象是 DataGridDetailsPresenter,没有指定名称
  • UI对象后面的DataContextnull
  • 绑定(bind)正试图找到 DataGrid 类型的 RelativeSource,以便它可以绑定(bind)到 AreRowDetailsFrozen 属性,但未能找到相对来源

因此,请仔细检查您的代码,寻找与此类似的内容:

<DataGridDetailsPresenter SelectiveScrollingOrientation="{Binding 
Path=AreRowDetailsFrozen,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}" />

第二个错误告诉你

  • 包含导致错误的绑定(bind)的属性是 Visibility
  • 包含问题属性的 UI 对象是 DataGridRowHeader,没有指定名称
  • UI对象后面的DataContextnull
  • 绑定(bind)试图找到类型为 DataGridRelativeSource,以便它可以绑定(bind)到 HeadersVisibility 属性,但未能找到相对来源

因此,请仔细检查您的代码,寻找与此类似的内容:

<DataGridRowHeader Visibility="{Binding 
Path=HeadersVisibility,
RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}" />

根据您发布的代码,第一个可能在您的 LeftScrollViewer 样式中,第二个可能在您的 DatagridColumnHeaderCustomTemplateStyle

如果您无法找到 XAML 中的错误,您可以尝试运行您的应用程序并使用类似 Snoop 的工具检查它。这将使您可以在 WPF 应用程序运行时查看它的 VisualTree,并且您应该能够在那里找到确切的绑定(bind)错误,以便您可以将其追溯到 XAML 中的源

关于c# - WPF 数据网格 [System.Windows.Data 错误 : 4],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15548662/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com