I'm new to WPF with MVVMLight and struggling to grasp how things work. I have a button in xaml:
<Button x:Name="button" Content="Button"
HorizontalAlignment="Left"
Margin="29,374,0,0"
VerticalAlignment="Top" Width="75"
Command="{Binding BeginCollectionCommand}"/>
And have the View Model respond to the button press.
BeginCollectionCommand = new RelayCommand(BeginCollectionCommandExecute, () => true);
I failed to find the answer to my question on
The View Model code has the button binding "BeginCollectionCommand" defined as
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"
}
Can someone help?
You questions could be sum up to three kinds of question.
For first and second question, bind your button IsEnable
to a property in viewModel and bind content to a string
In 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}"/>
In view model
// 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";
}
For the third question, you may use a timer or a ThreadSleep is okay.