2013-03-21 38 views
2

DataGrid WPF應用程序:WPF數據網格[System.Windows.Data錯誤:4]我已經如下所示

數據網格(簡化):

<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; 
     } 
    } 

的參數僅含有IDTimestamp和其它性質

一個 ObservableCollection

EDIT2: 的實例化在App.xaml中完成:

<ResourceDictionary> 
     <x:Static Member="vm2:CoreServiceLogView.CoreServiceLogViewCollection" 
          x:Key="CoreServiceCollection" /> 
    </ResourceDictionary> 

EDIT 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> 
+0

可以請你發佈你在CoreServiceCollection中有什麼嗎? – mchicago 2013-03-21 13:20:42

+0

我可以上傳更多,但我不認爲問題出在Collection – RayOldProf 2013-03-21 13:28:54

+0

,那麼您在哪裏創建CoreServiceLogViewCollection的實例?它是在資源中實例化的XAML中,還是已經附加在datacontext中? – mchicago 2013-03-21 13:29:49

回答

9

我回寫了一段關於how to read WPF binding errors的回答。基本上,打破你的錯誤的分號,並開始從下往上讀它,它應該給你一些想法,其中綁定錯誤是:

  • System.Windows.Data錯誤:4:
    • 找不到與參考'RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.DataGrid',AncestorLevel ='1'綁定的源代碼。 BindingExpression:路徑= AreRowDetailsFrozen;
  • DataItem = null;
  • 目標元素是'DataGridDetailsPresenter'(Name ='');
  • 目標屬性是 'SelectiveScrollingOrientation'(類型 'SelectiveScrollingOrientation')

  • System.Windows.Data錯誤:4:
    • 無法爲參考結合找到源' RelativeSource FindAncestor,AncestorType ='System.Windows.Controls.DataGrid',AncestorLevel ='1''。BindingExpression:路徑= HeadersVisibility;
  • DataItem = null;
  • 目標元素是'DataGridRowHeader'(Name ='');
  • 目標屬性是「可見性」(輸入「能見度」)

從底部向上閱讀,第一個錯誤是告訴你

  • 含有的結合導致錯誤的屬性是SelectiveScrollingOrientation
  • 包含問題屬性的UI對象是DataGridDetailsPresenter,沒有指定名稱
  • DataContext後面的UI對象是null
  • 結合試圖尋找DataGrid類型的RelativeSource,因此它可以綁定到AreRowDetailsFrozen財產,而其未能發現RelativeSource

通過您的代碼,以便尋找一些類似這樣的:

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

第二個錯誤是告訴你

  • 包含屬性結合導致該錯誤是Visibility
  • 包含有問題的屬性的UI對象是DataGridRowHeader,沒有名稱指定
  • 的UI對象背後的DataContextnull
  • 試圖尋找DataGrid類型的RelativeSource所以結合它可以綁定到HeadersVisibility財產,而其未能發現RelativeSource

通過您的代碼,以便尋找類似的東西這樣的:

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

根據您發佈的代碼,第一可能是你LeftScrollViewer風格的地方,第二個可能是你的DatagridColumnHeaderCustomTemplateStyle

如果你發現在你的XAML錯誤的問題,你可以嘗試運行您的應用程序並使用Snoop這樣的工具檢查它,它將讓您在運行時查看WPF應用程序的VisualTree,並且您應該能夠在那裏找到確切的綁定錯誤,以便您可以將其追溯回XAML中的源代碼

0

在ControlTemplate中爲DataGridRow發生此錯誤,該DataGridRow包含DataGridRowHeader和帶有上述綁定的DataGridDetailsPresenter。對於.NET 4.5 DataGrid,我遇到了同樣的問題。看起來,這兩個錯誤只發生在DataGrid使用虛擬化項目時,您可以嘗試在DataGrid中禁用它。在我看來,當DataGridRow以某種方式在虛擬化過程中從其DataGrid附加/分離時發生,並且綁定然後失去或者仍然沒有找到DataGrid的相對綁定目標。