c# - WPF Button Pressed/Released Binding -
i new wpf , trying best follow mvvm button im struggling current problem have view model class
public class mainviewmodel { private bool _reset; public bool reset{ get{ return _reset;} set {_reset = value;} } ... } now want bind button if while im pressing _reset true , when release _reset false feel command pattern alot of work simple on/off
is there way bind ispressed of button property data context
i want simple possible because have dozen or buttons doing type of thing other properties
so going need import system.windows.interactivity. go references, add reference, assemblies, extensions. find there. next add project
xmlns:inter="http://schemas.microsoft.com/expression/2010/interactivity"
you can use previewmouseleftbuttondown , 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"); } }
Comments
Post a Comment