0

I have searched thoroughly but no same question.

I am trying to make work community toolkit Mvvm ObservableObject ObservableProperty property with a WPF application.

It turns out that Mvvm ObservableProperty only works on fields not properties. Also we cant bind WPF elements to fields therefore I can only bind WPF ListBox into a property.

So here my question. How can I turn ObservableProperty field into bindable property and how can I bind it?

I know ObservableCollection but I am trying to make work ObservableProperty

Here my simple code behind

          public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public cstest myclass = new cstest();

    [INotifyPropertyChanged]
    public partial class cstest
    {
        [ObservableProperty]
        public List<string> _lstTest = new List<string>();
    
        public List<string> lstTest { get { return _lstTest; } }
    }
 

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        myclass._lstTest.Add(new Random().Next().ToString());

        var gg = myclass.lstTest;
    }

I am trying to bind lstTest to my ListBox on Main Window

 <ListBox Name="lstBox" ItemsSource="{Binding myclass.lstTest}" Margin="290,101,113,122"/>

But couldn't figure out yet. Thank you.

enter image description here

enter image description here

Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
  • I expected that user with 21K score understands what duplicate means (hint: it contains the solution). but no: https://stackoverflow.com/questions/74214006/how-can-i-turn-observableproperty-list-field-into-listbox-bindable-property-ne. don't blame community toolkit, when you can't make a property: `public cstest myclass = new cstest();` <- it is a field. and don't blame community toolkit when you use List instead of ObservableCollection. ObservableProperty for a property which never changes after initialization - it has zero sense – ASh Oct 26 '22 at 21:01
  • @ASh OK you have valid points. So can you give me an example that shows how can you utilize ObservableProperty in a wpf application to demonstrate same affect as ObservableCollection – Furkan Gözükara Oct 26 '22 at 21:09
  • "same affect as ObservableCollection" - what makes you think it is possible? they have different purposes. ObservableProperty is just a shortcut for implementation of INPC. in your example INPC isn't even necessary. – ASh Oct 26 '22 at 21:11
  • @ASh the naming makes me think. OK so what do I assumed is not possible I suppose? – Furkan Gözükara Oct 26 '22 at 21:16
  • @Clemens, I have already tried that: https://stackoverflow.com/questions/74214006/how-can-i-turn-observableproperty-list-field-into-listbox-bindable-property-ne. and after converting field to property, list has to be converted to OC, or added items won't be visible – ASh Oct 26 '22 at 21:21
  • @ASh I saw that, still the problem is here. – Clemens Oct 26 '22 at 21:22
  • And of course `lstTest` must be declared as ObservableCollection. ObservableProperty (besides incorrectly used) won't do anything useful here. It would not notify about collection changes. The question does not make much sense. – Clemens Oct 26 '22 at 21:24
  • @Clemens yes I know ObservableCollection. But I am trying to do same without using it if possible. – Furkan Gözükara Oct 26 '22 at 21:25
  • Not possible when you only add items to an existing collection. Create your own collection type that implements INotifyCollectionChanged when you don't want to use ObservableCollection. – Clemens Oct 26 '22 at 21:26
  • @Clemens I just made an ObservableProperty updating label content. However still working on List for ListBox – Furkan Gözükara Oct 26 '22 at 22:00
  • @Clemens ok i think i understand the logic now. ObservableProperty works only on non collection types right? – Furkan Gözükara Oct 26 '22 at 22:10
  • @ASh ok i think i understand the logic now. ObservableProperty works only on non collection types right? – Furkan Gözükara Oct 26 '22 at 22:18
  • 2
    That is a misconception. ObservableProperty is a helper to implement INotifyPropertyChanged - for any type of property. ObservableCollection implements INotifyCollectionChanged. When you add items to an existing collection object that is held by a property, this does not change the property, so firing the PropertyChanged event of INotifyPropertyChanged has no effect. You need to fire the CollectionChanged event of INotifyCollectionChanged, which ObservableProperty does not do. – Clemens Oct 27 '22 at 06:05
  • @Clemens are there equivalent of INotifyCollectionChanged in community tools like ObservableProperty ? – Furkan Gözükara Oct 27 '22 at 21:05

0 Answers0