Как использовать 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. 
        }
    }

Спасибо,

Имонна

Ответы

Ответ 1

DependencyObject - объект, который поднял событие. Вам нужно указать obj на нужный вам тип. Например.

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