WPF Button Pressed / Unlocked

I am new to WPF and try my best to follow the MVVM button. I am struggling with the current problem. I have a Model class.

public class MainViewModel
{
    private bool _Reset;
    public bool Reset{ get{ return _Reset;} set {_Reset = value;} }
    ...
}

Now I want to bind the button so that if Im pressing _Reset while it is true, and when I release it _Reset is false I feel that the command template is a lot of work for simple on / off

Is there a way to bind IsPressed buttons to a property from a data context

I want to make it as simple as possible, because I have a dozen or so buttons that make the type of thing only have other properties.

+4
source share
1 answer

, System.Windows.Interactivity. , , , . .

xmlns:inter="http://schemas.microsoft.com/expression/2010/interactivity"

PreviewMouseLeftButtonDown PreviewMouseLeftButtonUp.

<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");
    }
}
+4

Source: https://habr.com/ru/post/1626218/


All Articles