2010-03-03 15 views
6

में डेटाकॉन्टेक्स्ट पर बाध्यकारी मैं RadGridView में बाध्य कॉलम की एक श्रृंखला बनाने की कोशिश कर रहा हूं, और मैं दो स्तंभों में हाइपरलिंक बनाने के लिए टेम्पलेट का उपयोग कर रहा हूं। यहाँ मैं क्या है मूल रूप से है:डब्ल्यूपीएफ टेम्पलेट्स और एक ग्रिड व्यू

<telerik:GridViewDataColumn IsReadOnly="True" UniqueName="Distributor" DataContext="{Binding Distributor}" CellTemplate="{StaticResource linkTemplate}"/> 

और,

<DataTemplate x:Key="linkTemplate"> 
     <TextBlock> 
      <Hyperlink DataContext={TemplateBinding DataContext} Click="Hyperlink_Click"> 
       <TextBlock Text="{Binding Name}" /> 
      </Hyperlink> 
     </TextBlock> 
    </DataTemplate> 

RadGridView ही DistributorContainer वस्तुओं है कि अन्य बातों के अलावा है, एक वितरक संपत्ति का एक सेट के लिए बाध्य है। लिंक टेम्पलेट सीधे वितरक ऑब्जेक्ट में गुणों को संदर्भित करता है, इसलिए हाइपरलिंक के डेटाकॉन्टेक्स्ट को वितरक को सेट करने की आवश्यकता होती है।

दुर्भाग्यवश, हाइपरलिंक का डेटा संदर्भ वितरककंटर ऑब्जेक्ट है। मैं लिंक टेम्पलेट (साथ ही साथ हाइपरलिंक_Cलिक हैंडलर) का उपयोग कर रहा हूं जो वितरकों की सूचियों से जुड़ा हुआ है, और मैं वास्तव में इस टेम्पलेट का पुन: उपयोग करना चाहता हूं क्योंकि यह मूल रूप से वही बात है।

टेम्पलेट को अपने डेटाकॉन्टेक्स्ट के रूप में टेम्पलेट बाइंडिंग के माध्यम से GridViewDataColumn में क्यों नहीं मिल रहा है?

उत्तर

10

यह इस तरह प्राप्त करने के लिए एक उदाहरण है:

XAML

<Grid> 
    <Grid.Resources> 
     <DataTemplate x:Key="linkTemplate"> 
      <TextBlock> 
       <Hyperlink> 
        <TextBlock 
         Text="{Binding 
          Value.Name, 
           RelativeSource={RelativeSource FindAncestor, 
           AncestorType={x:Type telerik:GridViewCell}}}" /> 
       </Hyperlink> 
      </TextBlock> 
     </DataTemplate> 
    </Grid.Resources> 
    <telerik:RadGridView ItemsSource="{Binding}" AutoGenerateColumns="False"> 
     <telerik:RadGridView.Columns> 
      <telerik:GridViewDataColumn 
       DataMemberBinding="{Binding Distributor1}" 
       CellTemplate="{StaticResource linkTemplate}" /> 
      <telerik:GridViewDataColumn 
       DataMemberBinding="{Binding Distributor2}" 
       CellTemplate="{StaticResource linkTemplate}" /> 
     </telerik:RadGridView.Columns> 
    </telerik:RadGridView> 
</Grid> 

सी #

namespace WpfApplication1 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      DataContext = 
       from i in Enumerable.Range(0, 10) 
       select new DistributorContainer() 
       { 
        ID = i, 
        Distributor1 = new Distributor() { 
         Name = String.Format("Distributor1 Name{0}", i) }, 
        Distributor2 = new Distributor() { 
         Name = String.Format("Distributor2 Name{0}", i) } 
       }; 
     } 
    } 

    public class DistributorContainer 
    { 
     public int ID { get; set; } 
     public Distributor Distributor1 { get; set; } 
     public Distributor Distributor2 { get; set; } 
    } 

    public class Distributor 
    { 
     public string Name { get; set; } 
    } 
} 
+0

यह काम करता है और किसी भी gridview के लिए एक एकल टेम्पलेट का उपयोग कर के एक साधन प्रदान करता है, लेकिन है बस टेम्पलेट माता-पिता के डेटाकॉन्टेक्स्ट से जुड़ने का कोई तरीका है? यह टेम्पलेट को बहुत अधिक लचीलापन देगा। – Jake

+0

चूंकि टेम्पलेटेड पैरेंट (इस मामले में सेल) का डेटाकॉन्टेक्स्ट पंक्ति के डेटाकॉन्टेक्स्ट जैसा ही है, इसलिए आप वांछित परिणाम प्राप्त नहीं कर सकते हैं। –

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