CollectionPropertiesShouldBeReadOnly and Dependency Properti



我现在有一个依赖属性:

public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyPropertyDefaults", typeof(ICollection<string>), typeof(MyPanel), new PropertyMetadata(new List<string>()));
public ICollection<string> MyProperty
{
    get
    {
        return GetValue(MyPropertyProperty) as ICollection<string>;
    }
    set
    {
        this.SetValue(MyPropertyProperty, value);
    }
}

目的是该子面板将通过绑定传递一个列表,子面板操作该列表,然后父面板稍后可以读取该列表。例如

 <xaml:MyPanel MyProperty="{Binding MyPropertyList}" />

然而,FxCop报告CollectionPropertiesShouldBeReadOnly,我需要删除setter,这是属性所要求的。我该如何解决这个问题?做我正在做的事情的正确方法是什么?

像这样声明setter为private:

public class MyPanel : Panel
{
    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyPropertyDefaults", typeof(ICollection<string>), typeof(MyPanel), new PropertyMetadata(new List<string>()));
    public ICollection<string> MyProperty
    {
        get
        {
            return GetValue(MyPropertyProperty) as ICollection<string>;
        }
        private set
        {
            this.SetValue(MyPropertyProperty, value);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新