Enabe a button

Eduardo Gomez 3,426 Reputation points
2022-07-12T11:25:07.693+00:00

Hello

I am using two packages

  1. MVVM.Helpers (https://github.com/jamesmontemagno/mvvm-helpers)
  2. 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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,571 Reputation points Microsoft Vendor
    2022-07-14T05:54:31.327+00:00

    Hi,@Eduardo Gomez .

    You could update your code to <TextBox Grid.Column="1" Text="{Binding SelectedItem.name ,UpdateSourceTrigger=PropertyChanged}" /> .


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.