Change the enable of a button

Eduardo Gomez 4,156 Reputation points
2022-06-24T04:45:12.623+00:00

I have a command.cs, that check if all my input fields are not empty and base on that it is suppose to enable he button.

This is the command

public class SendDataCommand : ICommand {  

    public PrpertiesWindowViewModel _ViewModel { get; set; }  

    public SendDataCommand(PrpertiesWindowViewModel viewModel) {  
        _ViewModel = viewModel;  
    }  

    public event EventHandler? CanExecuteChanged {  
        add { CommandManager.RequerySuggested += value; }  
        remove { CommandManager.RequerySuggested -= value; }  
    }  

    public bool CanExecute(object? parameter) {  
        return _ViewModel.EnableButton;  
    }  

    public void Execute(object? parameter) {  
        _ViewModel.SendToServer();  
    }  
}  

This is my VM

[AddINotifyPropertyChangedInterface]
public class PrpertiesWindowViewModel {

    public Building? Building { get; set; }  

    public SendCommand? SendCommand { get; set; }  

    public PrpertiesWindowViewModel() {  

        SendCommand = new SendCommand(this);  

    }  

    public void SendData(Building building) {  
          
         
    }  
}  

UI

<Window.DataContext>  
        <vm:PrpertiesWindowViewModel />  
    </Window.DataContext>  
  
    <StackPanel>  
        <TextBox  
            materialDesign:HintAssist.Hint="Name of the building"  
            materialDesign:HintAssist.IsFloating="True"  
            Text="{Binding Building.Name, UpdateSourceTrigger=PropertyChanged}" />  
        <TextBox  
            x:Name="DescTx"  
            materialDesign:HintAssist.Hint="Description"  
            materialDesign:HintAssist.IsFloating="True"  
            Text="{Binding Building.Desc, UpdateSourceTrigger=PropertyChanged}" />  
        <TextBox  
            x:Name="RoomsTx"  
            materialDesign:HintAssist.Hint="Rooms"  
            materialDesign:HintAssist.IsFloating="True"  
            Text="{Binding Building.Rooms, UpdateSourceTrigger=PropertyChanged}" />  
        <TextBox  
            x:Name="AddressTx"  
            materialDesign:HintAssist.Hint="Address"  
            materialDesign:HintAssist.IsFloating="True"  
            Text="{Binding Building.Address, UpdateSourceTrigger=PropertyChanged}" />  
        <Button Command="{Binding SendCommand}"/>  

I am using MVVM, so I do not have/want code in my xaml.cs(CodeBehind)

app https://github.com/eduardoagr/RsidentsManager

Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Answer accepted by question author
  1. Hui Liu-MSFT 48,711 Reputation points Microsoft External Staff
    2022-06-24T08:34:09.857+00:00

    Hi,@Eduardo Gomez . Welcome Microsoft Q&A.
    For the problem of disabling the button when the text box in the window is null or empty, you could try the following method.

    Xaml:

       <Border Padding="20" Width="300">  
                <StackPanel>  
                    <TextBox  
                         materialDesign:HintAssist.Hint="Name of the building"  
                         materialDesign:HintAssist.IsFloating="True"  
                         Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />  
                    <TextBox  
                         materialDesign:HintAssist.Hint="Address"  
                         materialDesign:HintAssist.IsFloating="True"  
                         Text="{Binding Address,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />  
                    <Button Content="Save"  IsEnabled="{Binding IsEnabled}"   Command="{Binding ButtonCommand}"  />  
                </StackPanel>  
            </Border>  
    

    CodeBehind:

    214713-9.txt
    The result:

    214704-9.gif


    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 additional answers

Sort by: Most helpful

Your answer

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