I have a toggle buttons in my wpf application. On startup the togglebutton must be set.
My Xaml File:
<ToggleButton Content="AUD Reset" IsChecked="True" Height="23" HorizontalAlignment="Center" Margin="0" Name="button4" Command="{Binding Path=ConnectCommand}" VerticalAlignment="Center" Width="100" />
On togglebutton click I want to check for the toggle state in my viewmodel class and if it returns true then I want to do the following operation:
My ViewModel Class:
private ICommand mUpdater;
public ICommand ConnectCommand
{
get
{
if (mUpdater == null)
mUpdater = new DelegateCommand(new Action(ConnectToSelectedDevice), new Func<bool>(ConnectCanExecute));
return mUpdater;
}
set
{
mUpdater = value;
}
}
public bool ConnectCanExecute()
{
return true;
}
public void ConnectToSelectedDevice()
{
mComm.SetAddress(0x40);
Byte[] buffer= new Byte[2];
buffer[0] = 0x24;
buffer[1] = 0x00;
if(Check if button togglestate is set, if true then)
{
buffer[1] = 0x04;
}
mComm.WriteBytes(2, buffer);
}
How can I check whether the togglebutton is checked or not in my viewmodel and perform the above statements.
Please help!!
You can add IsChecked property to your ViewModel and bind it with ToggleButton.IsChecked dependency property:
Then check its state:
And finally, initialize IsChecked property in ViewModel’s constructor: