A
A
Anton Ivanov2021-05-10 18:57:24
WPF
Anton Ivanov, 2021-05-10 18:57:24

How to correctly solve CA2227:Collection properties should be read only for UserControl?

There is a usercontrol to display a collection in a listbox.

Here is the code snippet (using Fody.Bindables)

namespace UI.Controls.AdminSection.UsersPane
{
    [AddINotifyPropertyChangedInterface]
    public partial class ReferralsList : UserControl
    {
        public ReferralsList()
        {
            InitializeComponent();
        }

        [DependencyProperty]
        public ObservableCollection<Referral> Referrals { get; set; } =
            new ObservableCollection<Referral>();
    }
}


In xaml, this usercontrol is written like this (this is for the admin panel, referral partners associated with the selected user are shown):
<usersPane:ReferralsList
                    Referrals="{Binding ReferralsForSelectedUser}" />


At the same time, the warning pops up: CA2227: Collection properties should be read only

How to solve it correctly so that everything is "feng shui"?
I can't remove the setter from Referrals or make it private or replace it with a function.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2021-05-11
@yarosroman

Remove the assignment of an empty collection to a field, it is absolutely superfluous there, especially as I understand you are using PostSharp to generate DependencyProperty?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question