21

依存プロパティにバインドされた 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. 
    }
}
4

1 に答える 1

26

DependencyObject、イベントを発生させたオブジェクトです。必要な型にキャストobjする必要があります。例えば

TextBox textbox = (TextBox)obj;
textbox.ScrollToEnd();
于 2011-03-31T11:14:52.347 に答える