Silverlight:绑定到viewmodel的祖先类中定义的静态属性

时间:2017-02-21 10:48:22

标签: silverlight mvvm data-binding

在Silverlight中,使用MVVM我为相关的ViewModel定义了一个基类,并为几个子类中定义的属性定义了可能的值列表:

namespace MyNameSpace
{
    public class MyViewModelBase
    {
        public static List<MyPropertyClass> MyPropertyValueList
        {
            get
            {
                if (myPropertyValueList == null)
                {
                    // fill the list
                }
                return myPropertyValueList;
            }
        }
        private static List<MyPropertyClass> myPropertyValueList = null;
    }
}

现在我定义了我的ViewModel:

namespace MyNameSpace.MyChild
{
    public class MyViewModelChild
    {
        public MyPropertyClass MyProperty
        {
            get
            {
                return myProperty;
            }
            set
            {
                myProperty= value;
                RaisePropertyChanged("MyProperty");
            }
        }
        ...
    }
}

我绑定到我的ViewModel

<controls:ChildWindow
   x:Class="MyNameSpace.MyChild.MyChildEditor">
<ListBox ItemsSource="{Binding Path=MyPropertyValueList, Mode=OneTime}" SelectedValue="{Binding Path=MyProperty, Mode=TwoWay}"/>

然后MyPropertyValueList的绑定失败。 但是如果在子类中定义了MyPropertyValueList它就可以了。我做错了什么?

1 个答案:

答案 0 :(得分:1)

您将MyPropertyValueList定义为 static 属性。它不允许在Silverlight中使用。