2010-10-08 16 views
9

के साथ बाध्यकारी के लिए स्रोत नहीं मिल रहा है मुझे नीचे दिए गए कोड से निम्नलिखित त्रुटियां मिलती हैं ... सुनिश्चित नहीं है क्यों (और हाँ, यह सभी 4 उत्पन्न करता है भले ही यह वही 2 बार दोहराया गया हो)। ओह, और यह वैकल्पिक पंक्तियों का प्रभाव उत्पन्न नहीं करता है, भले ही इन त्रुटियों से पहले एक ही कोड पॉप-अप कर रहा था।डब्ल्यूपीएफ डेटा बाइंडिंग त्रुटि: संदर्भ 'रिलेटिवसोर्स 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

आपके 'आइटम स्रोत 'में ऑब्जेक्ट्स के लिए' DataTemplate' कहां है? –

+0

@ रॉबर्ट रॉसनी: मैंने इसे ऑटो जेनरेट कॉलम पर सेट किया है, इसलिए मुझे वास्तव में डेटा टेम्पलेट की आवश्यकता नहीं है। –

उत्तर

0

पहले, WPF datagrid के पंक्तियों डिफ़ॉल्ट रूप से सफेद होते हैं तो क्यों आप अपनी शैली में उन्हें सफेद सेट कर रहे हैं? आप DataGrid से छुटकारा पा सकते हैं। स्रोत पूरी तरह से बिट करते हैं और AlternatingRowBackground = "FFCAC6C6" के साथ AlternationCount = 2 को प्रतिस्थापित करते हैं (हालांकि इससे पहली पंक्ति सफेद हो जाएगी और दूसरा रंगीन हो जाएगा। यदि यह स्वीकार्य नहीं है तो भी आप हटा सकते हैं ट्रिगर जो पृष्ठभूमि को सफेद सेट करता है)।

त्रुटियों के बारे में - आपके द्वारा प्रदान किए RelativeSource सेट के साथ किसी भी बाइंडिंग शामिल नहीं है कोड के बाद से, मैं केवल दो चीजें निष्कर्ष निकाल सकते हैं:

1) या तो आप पूर्ण कोड प्रदान नहीं किया है और आप अपने फिर से विचार करने की जरूरत है बाइंडिंग जिनमें उनमें सापेक्ष स्रोत है, क्योंकि स्पष्ट रूप से कहीं भी एक त्रुटि है।

2) आप WPF के अंतर्निहित डेटाग्रिड का उपयोग नहीं कर रहे हैं। शायद कोडप्लेक्स से डब्ल्यूपीएफ टूलकिट डाटाग्रिड? हालांकि मुझे विश्वास है कि इसमें उन त्रुटियों को भी नहीं होना चाहिए, यह फिर से निष्कर्ष 1 की संभावना है।

+0

मैंने कोड की पूरी पोस्ट पोस्ट की है, हालांकि पोस्ट करने के लिए वास्तव में बहुत कुछ नहीं था। मैंने सुझाए गए AlternatingRowBackground में परिवर्तन किए हैं और यह ठीक काम करता है और अधिक संघनित है। मुझे अभी भी त्रुटियां मिलती हैं, हालांकि वे वास्तव में कार्यक्षमता या प्रदर्शन को प्रभावित नहीं करते हैं। ओह, और मैं अंतर्निहित डाटाग्रिड का उपयोग कर रहा हूं (डब्ल्यूपीएफ टूलकिट से नहीं)। –

+0

वहां कुछ कोड होना चाहिए जिसमें पूर्वजों के लुकअप मोड के साथ बाध्यकारी हो। 1) शायद xmlns में कुछ वर्ग: स्थानीय = "clr-namespace: MyProject.Views"? 2) या कुछ टेम्पलेट कहीं अधिक है ... 3) यदि आप डेटाग्रिड से आइटम्ससोर्स और चयनित वैल्यू बाइंडिंग को हटाते हैं तो क्या त्रुटियां गायब हो जाती हैं? 4) मुझे लगता है कि डाटाबेसिंग त्रुटियों की वर्बोजिटी को चालू करने के लिए दूर होना चाहिए, इसके लिए Google और शायद आपको और संकेत मिलेगा कि उन त्रुटियों को कहां फेंक दिया गया है। 5) चूंकि यह एक उपयोगकर्ता नियंत्रण है जिसे स्वयं पर डीबग नहीं किया जा सकता है, हो सकता है कि त्रुटियां होस्टिंग विंडो से आती हैं? – Marko

7

यह एक ज्ञात बग है; अधिक जानकारी के लिए http://wpf.codeplex.com/discussions/47047 और http://social.msdn.microsoft.com/Forums/en-GB/wpf/thread/af7cd462-febe-482b-9a04-61b076933c7b देखें।

पहले यूआरएल (कोडप्लेक्स) में मैं एक वर्कअराउंड पोस्ट करने जा रहा हूं; हालांकि, इसमें डब्ल्यूपीएफ टूलकिट स्रोत कोड में संशोधन शामिल है।

+0

ग्रेट लिंक, धन्यवाद। चूंकि हैकिंग डब्ल्यूपीएफ मेरे मामले में सवाल से बाहर है, शीर्ष से 9वीं पोस्ट> "मुझे मिला एकमात्र फिक्स हेडर विज़िबिलिटी =" ऑल "और रोहेडर विड्थ =" 0 "सेट करना है।" मेरे लिए पसंदीदा समाधान था। – Barton

संबंधित मुद्दे