【问题标题】:Silverlight: binding to a static property defined in an ancestor class of the viewmodelSilverlight:绑定到视图模型的祖先类中定义的静态属性
【发布时间】:2017-07-10 22:50:09
【问题描述】:

在 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,它就可以工作。我做错了什么?

【问题讨论】:

    标签: silverlight mvvm data-binding


    【解决方案1】:

    您将MyPropertyValueList 定义为静态 属性。 Silverlight 中不允许这样做。

    【讨论】:

    • 谢谢,如果我删除 static 关键字,它会起作用。但是,如果属性是在子类中定义的,为什么它也可以工作(使用 static 关键字)?
    猜你喜欢
    • 1970-01-01
    • 2010-09-27
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2013-07-22
    相关资源
    最近更新 更多