簡體   English   中英

如何使用 PropertyChangedCallBack

[英]How to use PropertyChangedCallBack

我有一個 TextBox 綁定到一個依賴屬性,我已經實現了一個 PropertyChangedCallBack 函數,當文本更改時我需要調用 textbox.ScrollToEnd() 但我不能,因為 PropertChanged 函數需要是靜態的,有沒有辦法解決這個問題?

static FrameworkPropertyMetadata propertyMetaData = new FrameworkPropertyMetadata
(
    "MyWindow",
    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
    new PropertyChangedCallback(TextProperty_PropertyChanged)
);

public static readonly DependencyProperty TextProperty = DependencyProperty.Register
(
    "TextProperty", 
    typeof(string), 
    typeof(OutputPanel),
    propertyMetaData
);

private void TextProperty_PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
    textbox.ScrollToEnd(); //An object reference is required for the non-static field.
}

public string Text
{
    get 
    { 
        return this.GetValue(TextProperty) as string;
    }
    set 
    { 
        this.SetValue(TextProperty, value);
        //textbox.ScrollToEnd(); // I originally called it here but I think it should be in the property changed function. 
    }
}

DependencyObject是引發事件的對象。 您需要將obj轉換為您需要的類型。 例如

TextBox textbox = (TextBox)obj;
textbox.ScrollToEnd();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM