如何绑定到在Silverlight本地属性,而无需使用一个DataContext? [英] How to bind to a local property in Silverlight without using a DataContext?

查看:140
本文介绍了如何绑定到在Silverlight本地属性,而无需使用一个DataContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,但都被发现相当多的不同,而且大多模棱两可,答案:

I have a small question but have been finding quite a few different, and mostly ambiguous, answers:

我有以下的用户控制和我试图绑定该控制(活动)范围内的公共财产。大家都说,我要使用的数据上下文,不过,我真的不希望这样做...我只是想绑定到财产...

I have the following user control and I am trying to bind to a public property within that control (Events). Everyone says that I have to use the data context, however, I don't really want to do that... I just want to bind to the property from within the control's XAML...

的要求是,结合具有为2的方式,以便在用户界面的任何变化将在属性被反映(或相当的集合)它必然。该集合中的每个事件的对象也实现INotifyPropertyChanged相同的方式,这种控制...

The requirement is that the binding has to be 2 way so any changes in the ui will be reflected in the property (or rather the collection) it is bound to. Each Event object within that collection also implements INotifyPropertyChanged the same way as this control...

任何想法,将不胜感激!

Any ideas would be greatly appreciated!

public partial class EventEditorWindow : UserControl, INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ObservableCollection<Event> events;
    public ObservableCollection<Event> Events
    {
        get { return this.events; }
        set
        {
            if( this.events != value )
            {
                this.events = value;
                this.RaisePropertyChanged("Events");
            }
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            this.VerifyPropertyName(propertyName);
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [Conditional("DEBUG")]
    [DebuggerStepThrough]
    public void VerifyPropertyName(string propertyName)
    {
        var currentObjectType = this.GetType();

        if (currentObjectType.GetProperty(propertyName) == null)
        {
            throw new ArgumentException("Property not found", propertyName);
        }
    }
}



谢谢,
Bleepzter。

Thanks, Bleepzter.

推荐答案

在构造函数中,设置的DataContext =这一点。这将有效地使你的DataContext背后的代码。据我所知,你不能完全避免的的东西的DataContext的。

In the constructor, set DataContext = this. That will effectively make your code behind your DataContext. AFAIK, you can't completely avoid making something the DataContext.

这篇关于如何绑定到在Silverlight本地属性,而无需使用一个DataContext?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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