I am new to WPF and trying to do my best to follow the MVVM button Im struggling with a current problem I have a view Model class
public class MainViewModel
{
private bool _Reset;
public bool Reset{ get{ return _Reset;} set {_Reset = value;} }
...
}
Now I want to bind a button so that if while Im pressing it _Reset is true and when I release it _Reset is false I feel like the command pattern is alot of work for a simple on/off
Is there a way to bind the IsPressed of a button to a property from my data context
I want to do this as simple as possible because I have a dozen or so buttons all doing the type of thing just other properties
So what you are going to need to do is import System.Windows.Interactivity. Go to references, add reference, Assemblies, Extensions. You will find it there. Next add it to your project
xmlns:inter="http://schemas.microsoft.com/expression/2010/interactivity"
you can now use the PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp event.
<Button Content="Some Button">
<inter:Interaction.Triggers>
<inter:EventTrigger EventName="PreviewMouseLeftButtonDown">
<inter:InvokeCommandAction Command="{Binding ButtonDown}"/>
</inter:EventTrigger>
<inter:EventTrigger EventName="PreviewMouseLeftButtonUp">
<inter:InvokeCommandAction Command="{Binding ButtonUp}"/>
</inter:EventTrigger>
</inter:Interaction.Triggers>
</Button>
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
ButtonDown = new RelayCommand(OnButtonDown);
ButtonUp = new RelayCommand(OnButtonUp);
}
public RelayCommand ButtonDown { get; set; }
public RelayCommand ButtonUp { get; set; }
private void OnButtonUp()
{
Debug.WriteLine("Button Released");
}
private void OnButtonDown()
{
Debug.WriteLine("Button Pressed");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With