简体   繁体   English

Silverlight模板化控件数据绑定到自定义属性

[英]Silverlight templated Control databinding to custom properties

Is there some trick that I'm missing here? 我在这里缺少一些技巧吗?

I've created a templated control, very simple. 我创建了一个模板控件,非常简单。 One single property on it, and I'd like to databind from the (viewmodel/datacontext of the) page in which it's hosted to a custom property on the control. 它上有一个单一属性,我想从托管该属性的(viewmodel / datacontext)页面进行数据绑定到控件的自定义属性。 The property will eventually be a vector type object, defining the position of the control, however in an attempt to get this to work I've tried reducing it to a basic string property. 该属性最终将是一个矢量类型的对象,定义了控件的位置,但是为了使它起作用,我尝试将其简化为基本的字符串属性。

Each time I'm faced with "Set property 'SimpleGame.Classes.Sprite.Property' threw an exception.". 每次遇到“设置属性'SimpleGame.Classes.Sprite.Property'引发异常”。

I can't even catch the exception in a debug session, the set property code is not being executed. 我什至无法在调试会话中捕获异常,因为未执行set属性代码。

Do I need to use a dependency / attached property or something? 我需要使用依赖项/附加属性吗? I wouldn't have thought so... 我不会这么想的

Can you give us some code sample. 您能给我们一些代码示例吗? Usualy when you try to bind a property it must be a dependency property or a property that use INotifyPropertyChanged Interface implements like ths 通常,当您尝试绑定属性时,它必须是依赖项属性或使用INotifyPropertyChanged接口实现的属性,例如

private string m_prop;
        public string Prop
        {
         get { return m_prop; }
         set { 
              m_prop = value; 
              NotifyPropertyChanged("Prop") 
          }
        }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM