gpt4 book ai didi

c# - 如何使用 PropertyChangedCallBack

转载 作者:IT王子 更新时间:2023-10-29 04:49:03 26 4
gpt4 key购买 nike

我有一个 TextBox 绑定(bind)到一个依赖属性,我已经实现了一个 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();

关于c# - 如何使用 PropertyChangedCallBack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5498517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com