【问题标题】:How to use PropertyChangedCallBack如何使用 PropertyChangedCallBack
【发布时间】:2011-03-31 10:36:41
【问题描述】:

我有一个绑定到依赖属性的 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. 
    }
}

【问题讨论】:

    标签: c# wpf dependency-properties propertychanged


    【解决方案1】:

    DependencyObject 是引发事件的对象。您需要将obj 转换为您需要的类型。例如。

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

    【讨论】:

      猜你喜欢
      • 2015-11-06
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      相关资源
      最近更新 更多