2012-06-01 16 views
8

सेट नहीं की जा रही है, मैं अपने कस्टम WPF नियंत्रण में XAML के माध्यम से एक निर्भरता संपत्ति को बांधने की कोशिश कर रहा हूं।डब्ल्यूपीएफ निर्भरता संपत्ति

यहाँ कैसे मैं निर्भरता संपत्ति रजिस्टर है:

public static readonly DependencyProperty AltNamesProperty = 
    DependencyProperty.Register ("AltNames", typeof(string), typeof(DefectImages)); 

public string AltNames 
{ 
    get { return (string) GetValue(AltNamesProperty); } 
    set { SetValue(AltNamesProperty, value); } 
} 

और यहाँ कैसे मैं इसे अपने XAML में फोन है:

<DataGrid.Columns>     
    <DataGridTemplateColumn IsReadOnly="True"> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <StackPanel Name="StackPanel1" Grid.Column="0" Width="950"> 
        <TextBlock FontSize="16" TextDecorations="None" Text="{BindingPath=StandardName}" Foreground="Black" FontWeight="Bold" Padding="5,10,0,0"></TextBlock> 
        <TextBlock Text="{Binding Path=AltNames}"TextWrapping="WrapWithOverflow" Padding="5,0,0,10"></TextBlock> 
        <!-- this part should be magic!! --> 
        <controls:DefectImages AltNames="{Binding Path=AltNames}"></controls:DefectImages> 
       </StackPanel> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
</DataGrid.Columns> 

मैं AltNames संपत्ति है कि मैं है करने के लिए बाध्य करने का प्रयास कर रहा हूँ पता है एक वैध संपत्ति क्योंकि मैं इसे टेक्स्टब्लॉक में बस ठीक से प्रदर्शित कर सकता हूं। क्या मैं निर्भरता संपत्ति को गलत तरीके से पंजीकृत कर रहा हूं?

मेरे कोड में AltNames पर दिए गए सही मूल्य को प्राप्त करने के लिए मुझे क्या करने की आवश्यकता है?

+0

आप की कोशिश की है 'AltNames =" {बाइंडिंग पथ = AltNames, मोड = TwoWay} "':
यहाँ मैं अंत में साथ समाप्त हो गया है? रनटाइम के दौरान आउटपुट विंडो में कोई बाध्यकारी त्रुटियां हैं? – nemesv

+0

@nemesv ने किसी भी भाग्य के साथ बाध्यकारी दो तरीकों की कोशिश की। आउटपुट विंडो में कोई बाध्यकारी त्रुटियां नहीं हैं। – jacobsimeon

+0

यह संभावना है कि यह समान नाम साझा करने वाले 2 गुणों से भ्रमित हो रहा है। क्या आपने एक नाम बदलने का प्रयास किया? एक जवाब के रूप में आप जिस कामकाज को ध्वजांकित करते हैं वह बेकार लगता है! – Gusdor

उत्तर

11

मुझे शुरू करने के लिए @ डैंको के लिए धन्यवाद। संपत्ति बदलते समय मूल्य निर्धारित करने के लिए मैंने एक कॉलबैक पंजीकृत किया।

private static void OnDefectIdChanged(DependencyObject defectImageControl, DependencyPropertyChangedEventArgs eventArgs) 
{ 
    var control = (DefectImages) defectImageControl; 
    control.DefectID = (Guid)eventArgs.NewValue; 
} 

/// <summary> 
/// Registers a dependency property, enables us to bind to it via XAML 
/// </summary> 
public static readonly DependencyProperty DefectIdProperty = DependencyProperty.Register(
    "DefectID", 
    typeof (Guid), 
    typeof (DefectImages), 
    new FrameworkPropertyMetadata(
     // use an empty Guid as default value 
     Guid.Empty, 
     // tell the binding system that this property affects how the control gets rendered 
     FrameworkPropertyMetadataOptions.AffectsRender, 
     // run this callback when the property changes 
     OnDefectIdChanged 
    ) 
    ); 

/// <summary> 
/// DefectId accessor for for each instance of this control 
/// Gets and sets the underlying dependency property value 
/// </summary> 
public Guid DefectID 
{ 
    get { return (Guid) GetValue(DefectIdProperty); } 
    set { SetValue(DefectIdProperty, value); } 
} 
2

हो सकता है कि आपको PropertyMetadata तर्क DependencyProperty.Register पर तर्क निर्दिष्ट करने की आवश्यकता हो, यदि संपत्ति प्रभावित करती है कि नियंत्रण कैसे प्रदान किया जाता है। उदाहरण के लिए:

DependencyProperty.Register("AltNames", typeof(string), typeof(DefectImages), 
           new FrameworkPropertyMetadata(null, 
           FrameworkPropertyMetadataOptions.AffectsRender)); 
+0

कोशिश की, और यह अभी भी मेरे लिए नहीं जा रहा है। मैंने AltNames प्रॉपर्टी के लिए सेटटर पर ब्रेकपॉइंट रखा और इसे कभी भी निष्पादित नहीं किया गया। – jacobsimeon

+0

खैर, सेटटर वास्तव में कभी निष्पादित नहीं होता है, क्योंकि बाध्यकारी "दृश्यों के पीछे" किया जाता है। आप [इस संपत्ति मेटाडेटा कन्स्ट्रक्टर] (http://msdn.microsoft.com/en-us/library/ms557327.aspx) को कॉलबैक विधि निर्दिष्ट करने का प्रयास कर सकते हैं जिसे संपत्ति बदलते समय निष्पादित किया जाता है। साथ ही, जांचें [मैं डब्ल्यूपीएफ बाइंडिंग कैसे डिबग कर सकता हूं?] (Http://bea.stollnitz.com/blog/?p=52)। –

+0

यह वही है जो मैंने अभी किया था। मेरे लिए काम मूल्य निर्धारित करने के लिए कॉलबैक का उपयोग करना। मुझे सही रास्ते पर लाने के लिए धन्यवाद। – jacobsimeon

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