2010-11-20 24 views
0

Oldukça emin bir şekilde yanlış bir şey yapıyorum, ancak bunu anlayamıyorum.WPF Bağlama Çalışmıyor

Bir sınıfın etrafında basit bir sarıcı oluşturdum ve bağımlılık özelliği ekledim, böylece bağlanabilirdim. Ancak, bağlanma hiçbir hata vermez, ancak hiçbir şey yapmaz.

Öğeleri basitleştirmek için, sınıfı TextBox olarak değiştirdim ve aynı sonuçları aldım.

public class TextEditor : TextBox 
{ 
    #region Public Properties 

    #region EditorText 
    /// <summary> 
    /// Gets or sets the text of the editor 
    /// </summary> 
    public string EditorText 
    { 
     get 
     { 
     return (string)GetValue(EditorTextProperty); 
     } 

     set 
     { 
     //if (ValidateEditorText(value) == false) return; 
     if (EditorText != value) 
     { 
      SetValue(EditorTextProperty, value); 
      base.Text = value; 

      //if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("EditorText")); 
     } 
     } 
    } 

    public static readonly DependencyProperty EditorTextProperty = 
     DependencyProperty.Register("EditorText", typeof(string), typeof(TextEditor)); 
    #endregion 

    #endregion 

    #region Constructors 

    public TextEditor() 
    { 
     //Attach to the text changed event 
     //TextChanged += new EventHandler(TextEditor_TextChanged); 
    } 

    #endregion 

    #region Event Handlers 

    private void TextEditor_TextChanged(object sender, EventArgs e) 
    { 
     EditorText = base.Text; 
    } 

    #endregion 
} 

Ben ilk sonuçlar verir aşağıdaki XAML çalıştırmak

, ama ikinci bir (EditorText) bile EditorText özelliği isabet etmez.

<local:TextEditor IsReadOnly="True" Text="{Binding Path=RuleValue, Mode=TwoWay}" WordWrap="True" /> 
<local:TextEditor IsReadOnly="True" EditorText="{Binding Path=RuleValue, Mode=TwoWay}" WordWrap="True" /> 

cevap

4

CLR özelliğinizde fazladan iş yapıyorsunuz. CLR mülkünüzün WPF tarafından kullanılacağına dair bir garanti yoktur, bu yüzden bunu yapmamalısınız. Bunun yerine, aynı etkiyi elde etmek için DP'nizdeki meta verileri kullanın.

public string EditorText 
{ 
    get { return (string)GetValue(EditorTextProperty); } 
    set { SetValue(EditorTextProperty, value); } 
} 

public static readonly DependencyProperty EditorTextProperty = 
    DependencyProperty.Register(
     "EditorText", 
     typeof(string), 
     typeof(TextEditor), 
     new FrameworkPropertyMetadata(OnEditorTextChanged)); 

private static void OnEditorTextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) 
{ 
    var textEditor = dependencyObject as TextEditor; 

    // do your extraneous work here 
} 
+0

+1, bana onu dövdün :) –

+0

Tek kelimeyle harika. Bunu hiç bir yerde belgelemedim, ama harika çalışıyor. Teşekkürler. – Telavian