如何使用PropertyChangedCallBack [英] How to use PropertyChangedCallBack

查看:1343
本文介绍了如何使用PropertyChangedCallBack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须绑定到一个物业扶养一个TextBox,我实现了一个PropertyChangedCallBack功能,当文本更改我需要调用textbox.ScrollToEnd(),但我不能,因为PropertChanged功能必须是静态的,是有办法解决办法此?

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. 
        }
    }



谢谢,

Thanks,

埃蒙·

推荐答案

的DependencyObject 是对象引发事件。你需要投 OBJ 到你需要的类型。例如。

The DependencyObject is the object that raised the event. You need to cast obj to the type you need. E.g.

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

这篇关于如何使用PropertyChangedCallBack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆