the button dosent get reevaluated to see if it can get press

Eduardo Gomez 2,916 Reputation points
2022-10-20T20:47:21.343+00:00

For some reason, I can't activate my button.

In my app, I have a textbox, that gets populated with the file path that the user chooses from the dialog
I also have a combo box, to let the user choose the language

What I want to do, is to Activate my button "Start", when the FilePath is not empty and the user selects a language.

   [AddINotifyPropertyChangedInterface]  
       public class AudioPageViewModel {  
     
           public string? FilePath { get; set; }  
     
           public bool IsWorking { get; set; }  
     
           public bool CanPress { get; set; }  
     
           public string? SelectedItem { get; set; }  
     
           public List<string>? Languages { get; set; }  
     
           public Visibility CanShow { get; set; }  
     
           public DialogHelper Dialog { get; }  
     
           public Command PickFileCommad { get; set; }  
     
           public Command StartCommand { get; set; }  
     
           public AudioPageViewModel() {  
               InitListLanguages();  
               Dialog = new DialogHelper();  
               CanShow = Visibility.Hidden;  
               PickFileCommad = new Command(PickFileAction);  
               StartCommand = new Command(StartAction, CanStartAction);  
           }  
     
           private bool CanStartAction(object arg) {  
               if (string.IsNullOrEmpty(SelectedItem) ||  
                   string.IsNullOrEmpty(FilePath)) {  
                   return false;  
               }  
               return true;  
           }  
     
           private void StartAction(object obj) {  
               throw new NotImplementedException();  
           }  
     
           private void PickFileAction() {  
               var filePath = Dialog.GetFilePath(ConstantsHelpers.AUDIO);  
               FilePath = filePath;  
           }  
     
           private void InitListLanguages() {  
     
               Languages = new List<string>() {  
                   "English",  
                   "Spanish"  
               };  



   <Grid Margin="20">  
           <Grid.ColumnDefinitions>  
               <ColumnDefinition Width="Auto" />  
               <ColumnDefinition />  
           </Grid.ColumnDefinitions>  
           <Grid.RowDefinitions>  
               <RowDefinition />  
               <RowDefinition />  
               <RowDefinition />  
               <RowDefinition />  
               <RowDefinition />  
           </Grid.RowDefinitions>  
     
           <Button  
               VerticalAlignment="Top"  
               Command="{Binding PickFileCommad}"  
               Content="Pick a file"  
               FontWeight="Bold" />  
     
           <Label  
               Grid.Row="1"  
               Margin="0,5,0,0"  
               VerticalContentAlignment="Top"  
               Content="language of the file" />  
     
           <TextBox  
               Grid.Column="1"  
               Margin="10,0,10,0"  
               VerticalAlignment="Top"  
               IsReadOnly="True"  
               Text="{Binding FilePath}" />  
     
           <ComboBox  
               Grid.Row="1"  
               Grid.Column="1"  
               Margin="10,0,10,0"  
               SelectedItem="{Binding SelectedItem}"  
               HorizontalAlignment="Stretch"  
               ItemsSource="{Binding Languages}">  
           </ComboBox>  
     
           <Label  
               Grid.Row="3"  
               Grid.ColumnSpan="2"  
               Margin="10,0,10,0"  
               HorizontalContentAlignment="Center"  
               VerticalContentAlignment="Center"  
               Content="This will not take long"  
               Visibility="{Binding CanShow}" />  
     
           <ui:ProgressRing  
               Grid.Row="2"  
               Grid.ColumnSpan="2"  
               Width="60"  
               Height="60"  
               Margin="10,0,10,0"  
               IsActive="{Binding IsWorking}" />  
     
           <Button  
               Grid.Row="4"  
               Grid.ColumnSpan="2"  
               Margin="10,0,10,10"  
               HorizontalAlignment="Stretch"  
               VerticalAlignment="Bottom"  
               Content="Start"  
               Command="{Binding StartCommand}"  
               IsEnabled="{Binding CanPress}" />  
       </Grid>  
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,564 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 23,606 Reputation points Microsoft Vendor
    2022-10-26T06:36:50.057+00:00

    I added codebehind based on your UI. You could refer to it.

    The codebehind:

    254140-active-enable-button-command.txt

    The resutl:
    254149-22.gif

    ----------------------------------------------------------------------------

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

    0 comments No comments

0 additional answers

Sort by: Most helpful