MVVMLight C# как изменить содержимое кнопки - PullRequest
0 голосов
/ 08 марта 2020

Я новичок в WPF с MVVMLight и пытаюсь понять, как все работает. У меня есть кнопка в xaml:

<Button x:Name="button" Content="Button" 
    HorizontalAlignment="Left" 
    Margin="29,374,0,0" 
    VerticalAlignment="Top" Width="75" 
    Command="{Binding BeginCollectionCommand}"/>

, и View Model реагирует на нажатие кнопки. BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () => true);

Мне не удалось найти ответ на мой вопрос по

  • Как настроить кнопку для отключения
  • Как установить для "content =" значение "working ..."
  • Как повторно включить кнопку после завершения проекта
  • Как установить для "content =" значение "Done"
  • Я также хочу подождать 5 секунд, чтобы снова установить содержимое на «Пуск». Я полагаю, что могу сделать это с помощью thread.sleep (5000), но с остальными частями, которые мне не ясны.

В коде View Model есть привязка кнопки «BeginCollectionCommand», определенная как

public RelayCommand BeginCollectionCommand { get; set; }
    public MainWindowViewModel()
  {
    BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () => true);
    //at this point i believe is where i set the button content to "working..."
    //and disable.
  }

  public void BeginCollectionCommandExecute()
  {
    /// do my working class code

    //I think at this point I want to set the code to change button content to
    //enable, conent to "done" then wait and set to "start"
  }

Может кто-нибудь помочь?

1 Ответ

1 голос
/ 09 марта 2020

Ваши вопросы могут быть суммой до трех видов вопросов.

  1. Как включить или отключить кнопку.
  2. Как изменить содержимое кнопки.
  3. Как изменить содержимое после определенного периода времени.

Для первого и второго вопроса свяжите свою кнопку IsEnable со свойством в viewModel и свяжите содержимое со строкой

В xaml.

<Button x:Name="button" 
    Content="{Binding ButtonString}"
    HorizontalAlignment="Left" 
    Margin="29,374,0,0" 
    VerticalAlignment="Top" Width="75"
    IsEnabled="{Binding ButtonEnabled}"
    Command="{Binding BeginCollectionCommand}"/>

В представлении модель

    // Set true or button cannot be pressed.
    bool m_Enabled = true;
    public bool ButtonEnabled
    {
        get{  return m_Enalbed; }
        set{ m_Enabled = value; 
         // RaisePropertyChanged MUST fire the same case-sensitive name of property
             RaisePropertyChanged( "ButtonEnabled" );
           }
        }
    }

    public bool ButtonString
    {
     get;set;
    }
    bool m_String = false;
    public bool ButtonString
    {
        get{  return m_String; }
        set{ m_String = value; 
             // RaisePropertyChanged MUST fire the same case-sensitive name of property
             RaisePropertyChanged( "ButtonString" );
           }
        }
    }


public void BeginCollectionCommandExecute()
{
    //I simplify the way of variable passing, 
    //You need to take care of how to set property from command to viewmodel. 
    //A method delegate would be okay.
    ButtonEnabled = false;
    ButtonString = "Working";
    // working here
    ButtonEnabled = true;
    ButtonString = "Done";
}

Для третьего вопроса вы можете использовать таймер или ThreadSleep - это нормально.

...