7

i am using CommunityToolkit to configure the binding properties in the view model, but in some cases I would like to set the property only as readonly. So I am tryiung this:

[ObservableProperty]
public MyType _myProperty{ get; private set; }

But this is only possible with fields, not with properties.

Is it possible to set a readonly property with CommunityToolkit?

Thanks.

6
  • I wonder if ObservableProperty is the incorrect thing for you here then, since the general idea behind using that is that you get the property change notifications and all the other goodness wired up automatically. Can you explain a little more about how you're using this, and why you wouldn't want it to be set from your view? Commented Oct 18, 2023 at 5:00
  • Well, I would like that the property would be updated only from the view model, no from the view. In fact, in the view you can set the type of binding as two ways or only one direction, so what I would like to want it is to set the property in the view model as read only, to avoid the need to trust that the view will be setted in the correct way. Commented Oct 18, 2023 at 9:24
  • 1
    I’m on mobile so forgive the formatting but if you really need to do this, theoretically you may be able to implement a partial OnMyPropertyChanging method and try to do something there to prevent the change. Maybe newValue = oldValue or some such. Not sure if that would work… Commented Oct 19, 2023 at 0:47
  • Yes, it is an option to implement PropertyChanging instead to wait until PropertyChanged. I will follow this way. Really it is a particular case. Commented Oct 19, 2023 at 10:16
  • Observable properties (and indeed INotifyPropertyChanged itself) aren't only intended for ViewModels - they're fantastic for use in data models. Many data models want to expose properties as readonly to indicate the value can only be changed internal to the model, or via a public function. As such it would be great if we could define something like [ObservableProperty(isSetterPrivate: true)]. Commented Sep 30, 2024 at 23:14

3 Answers 3

3

What you are looking for is something like:

[ObservableProperty(IsReadOnly = true)]
public MyType _myProperty{ get; private set; }

To my knowledge the current implementation doesn't support the feature you are looking for. I have no idea why they didn't implement it. The attribute is used by the source generator to generate a property. It could easily read the parameter and generate a private setter. Maybe we should make a feature request.

Sign up to request clarification or add additional context in comments.

Comments

1

Since CommunityToolkit.Mvvm v8.4, partial properties are supported, and we can now write:

[ObservableProperty]
public partial MyType MyProperty{ get; private set; }

In XAML, the compiler won't let you bind this on TwoWay mode.

Comments

0

You can implement a read property or a computed property. But unfortunately, ToolKit does not have built-in tools to simplify such implementation. Therefore, you will have to implement it with hand-written code.
An example of such an implementation:

        private readonly SomeModel model;

        public PeopleViewModel(SomeModel model)
        {
            this.model = model;
            model.NameChanged += OnNameChanged;
        }

        public string ModelName => model.Name;

        private void OnNameChanged(object? sender, EventArgs e)
        {
            OnPropertyChanged(nameof(ModelName));
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.