Reputation: 885
I am pretty new to WPF and MVVM so this may be a very easy question. I have an app with a button and a checkbox. Once the button is clicked it runs a command that then runs a script. The checkbox is an option to view an internet browser as the script runs. I am wondering how I can pass in wheather the checkbox is checked or not once the button is selected. I changed some of the coding names to be more basic. Here is my Xaml:
<StackPanel Margin="10">
<CheckBox Content="Option" IsChecked="True" />
<Button Height="20"
Content="Run Script"
Command="{Binding Script }"
/>
</StackPanel>
And here is the the ViewModel:
class MainWindowViewModel
{
public ICommand script{ get; set; }
public MainWindowViewModel()
{
script = new RelayCommand(o => MainButtonClick());
}
private void MainButtonClick()
{
Program start = new Program();
start.Begin();
}
}
Upvotes: 1
Views: 1028
Reputation: 7855
You can bind the IsChecked
of the CheckBox to a property in the ViewModel. Something like this should work:
<CheckBox Content="Option" IsChecked="{Binding ShowBrowser}" />
public bool ShowBrowser {get; set;}
You can then use the ShowBrowser
property in your MainButtonClick
method
Or you could use a Command Parameter as dymanoid pointed out in the comments. Like so:
<CheckBox Name="ShowBrowser" Content="Option" IsChecked="True" />
<Button Height="20"
Content="Run Script"
Command="{Binding Script }"
CommandParameter="{Binding ElementName=ShowBrowser, Path=IsChecked}
/>
And then your Method would look like this:
private void MainButtonClick(bool showBrowser)
{
Program start = new Program();
start.Begin();
}
This is of course assuming your RelayCommand
class can handle parameters
Upvotes: 3