Hello
I am using two packages
- MVVM.Helpers (https://github.com/jamesmontemagno/mvvm-helpers)
- Fody.Propertyhange (https://github.com/Fody/PropertyChanged)
Fody will implement INotifyPropertyChanged, so you do not need to do a backing field for every property.
MVVM helpers will expose the commands, async commands, etc.
I cannot enable the button, based on the text in the textbox
ViewModel
[AddINotifyPropertyChangedInterface]
public class MainWindowViewModel
{
public Command MyProperty
{
get; set;
}
public AirportServices Services
{
get; set;
}
public ObservableCollection<Airport> AirportsList
{
get; set;
}
public Airport? SelectedItem
{
get; set;
}
public MainWindowViewModel()
{
SelectedItem = new Airport
{
OnAnyPropertiesChanged = () => { MyProperty?.RaiseCanExecuteChanged(); }
};
Services = new AirportServices();
AirportsList = new ObservableCollection<Airport>();
MyProperty = new Command(Do,CanDo);
GetAirportList();
}
private bool CanDo(object arg)
{
return SelectedItem != null && !string.IsNullOrEmpty(SelectedItem.name);
}
private void Do(object obj)
{
MessageBox.Show("sdc");
}
private async void GetAirportList()
{
var temppList = await Services.GetAirportsAsync("https://gist.githubusercontent.com/tdreyno/4278655/raw/7b0762c09b519f40397e4c3e100b097d861f5588/airports.json");
var airports = new ObservableCollection<Airport>();
foreach (var item in temppList!)
{
airports.Add(item);
}
AirportsList = airports;
}
UI
<TextBox Grid.Column="1" Text="{Binding SelectedItem.name}" />
<Button
Grid.Row="2"
Grid.Column="1"
Command="{Binding MyProperty, UpdateSourceTrigger=PropertyChanged}" />
I do not want to duplicate the Airport Births in my ViewModel. That is the easiest solution, but I want to d it this way.
Why is not working
App https://github.com/eduardoagr/Travel