如何使用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.
}
}
I have a TextBox Binded to a dependancy property, I have implemented a PropertyChangedCallBack function, when the text changes I need to call textbox.ScrollToEnd() but I cant since the PropertChanged function need to be static, is there a way around this?
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.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DependencyObject
是引发事件的对象。您需要将 obj 转换为您需要的类型。例如The
DependencyObject
is the object that raised the event. You need to castobj
to the type you need. E.g.