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.