2010-10-08 4 views
9

Ottengo i seguenti errori dal codice seguente ... non so perché (e sì, produce tutti i 4 anche se è lo stesso 2 ripetuto). Oh, e non produce l'effetto di righe alternate, anche se prima di questi errori stava spuntando lo stesso codice funzionante.Errore DataBinding WPF: impossibile trovare l'origine per il binding con riferimento 'RelativeSource FindAncestor'

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') 
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') 
<UserControl x:Class="MyProject.Views.RegistrationAllView" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:MyProject.Views" 
      > 
    <Grid> 
     <DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="True" 
        ItemsSource="{Binding Registrations}" SelectedValue="{Binding CurrentRegistration}" IsReadOnly="True" GridLinesVisibility="None" 
        AlternatingRowBackground="#FFCAC6C6" 
        > 
      <DataGrid.RowStyle> 
       <Style> 
        <EventSetter Event="DataGridRow.MouseDoubleClick" Handler="TestGrid_MouseDoubleClick" /> 
       </Style> 
      </DataGrid.RowStyle> 
     </DataGrid> 
    </Grid> 
</UserControl> 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

using MyProject.ViewModels; 

using WPFBase; 
using WPFBase.ViewModels; 

namespace MyProject.Views 
{ 
    public partial class RegistrationAllView : UserControl 
    { 
     public RegistrationAllView() 
     { 
      InitializeComponent(); 
     } 

     private void TestGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
     { 
      DependencyObject source = e.OriginalSource as DependencyObject; 

      RegistrationEntity entity = (RegistrationEntity)TestGrid.CurrentItem; 

      TabControl TabCollection = (TabControl)UIHelper.TryFindParentControl<TabControl>(this); 

      RegistrationForm view = new RegistrationForm(); 

      XTabItem tabItem = new XTabItem(); 
      tabItem.Header = String.Format("Registration (#{0})", entity.ID); 
      tabItem.Content = view; 

      TabCollection.Items.Add(tabItem); 

      tabItem.Focus(); 

      AbstractViewModel vm = new RegistrationViewModel(entity); 

      view.DataContext = vm; 
     } 
    } 
} 
+0

Dov'è il 'DataTemplate' per gli oggetti nel tuo' ItemsSource'? –

+0

@Robert Rossney: l'ho impostato su AutoGenerateColumns, quindi non ho davvero bisogno di un DataTemplate. –

risposta

0

Prima di tutto, le righe del DataGrid WPF sono bianchi di default così perché stai impostando hanno imbiancate nel tuo stile? Si può sbarazzarsi del bit DataGrid.Resources completamente e sostituire AlternationCount = 2 con AlternatingRowBackground = "FFCAC6C6" (anche se questo farà sì che la prima riga sia bianca e la seconda da colorare ecc. Se ciò non è accettabile, è comunque possibile eliminare il grilletto che imposta lo sfondo bianco).

Circa gli errori - dato che il codice tua condizione non contiene alcun binding con set RelativeSource, posso solo concludere due cose:

1) o non ha fornito il codice completo ed è necessario relook tua binding che hanno RelativeSource in loro, poiché chiaramente c'è un errore da qualche parte.

2) Non si sta utilizzando il DataGrid integrato di WPF. Forse il toolkit WPF DataGrid di codeplex? Anche se credo che non dovrebbe avere neanche quegli errori, è più probabile la conclusione 1 di nuovo.

+0

Ho pubblicato l'intero codice, anche se non c'era molto altro da pubblicare. Ho apportato modifiche a AlternatingRowBackground come suggerito e funziona bene ed è più condensato. Ricevo ancora gli errori, anche se non sembrano influenzare la funzionalità o il display. Oh, e sto usando il Datagrid incorporato (non quello del WPF Toolkit). –

+0

Ci deve essere un po 'di codice che ha un legame con la modalità di ricerca degli antenati. 1) Forse qualche classe in xmlns: local = "clr-namespace: MyProject.Views"? 2) O qualche template è sovrascritto da qualche parte ... 3) Se rimuovi i collegamenti ItemsSource e SelectedValue dal datagrid, gli errori scompaiono? 4) Penso che ci dovrebbe essere una via per alzare la verbosità degli errori di databinding, google per esso e forse avrai più indizi su dove vengono lanciati quegli errori. 5) Poiché si tratta di un controllo utente che non può essere sottoposto a debug da solo, forse gli errori provengono dalla finestra di hosting? – Marko

7

Questo è un bug noto; controllare http://wpf.codeplex.com/discussions/47047 e http://social.msdn.microsoft.com/Forums/en-GB/wpf/thread/af7cd462-febe-482b-9a04-61b076933c7b per ulteriori dettagli.

Nel primo URL (Codeplex) ho intenzione di pubblicare una soluzione alternativa; tuttavia, ciò comporta la modifica del codice sorgente di WPF Toolkit.

+0

Ottimo link, grazie. Dal momento che l'hacking di WPF è fuori questione nel mio caso, il 9 ° post dalla parte superiore> "L'unica correzione che ho trovato è impostare HeadersVisibility =" All "e RowHeaderWidth =" 0 "." era la soluzione preferita per me. – Barton