Using command bindings in your projects can make our code more compliant with MVVM mode. Students who do not understand may not be aware that only elements that inherit from the ButtonBase class can bind the command directly (Button, CheckBox, RadioButton, etc.)
<button content= "Normal" command= "{Binding Normaleventcommand}" ></Button>
If we're going to work with labels or some other controls, we can only walk the event:
<label content= "Label Event" mousedoubleclick= "Label_mousedoubleclick_1" ></Label>
In this case, we have to deal with the event code and part of the logic in the form class so that we don't get a clean mvvm pattern, so what should we do?
Blend provides us with solutions, we can get to System.Windows.Interactivity.dll after we install blend, add the DLL to the project reference
<window x:class= "Wpfapplication1.window2" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i= "Clr-namespace:system.windows.interactivity;a Ssembly=system.windows.interactivity "xmlns:vm=" Clr-namespace:wpfapplication1 "title=" Window2 "Height=" 124
"Width=" 214 "> <Window.DataContext> <vm:window2viewmodel/> </Window.DataContext> <Grid> <button name= "btn" content= button "height=" horizontalalignment= "left" margin= "40,24,0,0" Ve Rticalalignment= "Top" width= "109" > <i:interaction. triggers> <i:eventtrigger eventname= "click" > <i:invokecommandaction comman D= "{Binding Command1}" commandparameter=/> </i:EventTrigger> <i:eventtri Gger eventname= "MouseMove" > <i:invokecommandaction COmmand= ' {Binding Command2} ' commandparameter= ' {Binding elementname=btn} '/> </i:EventTrigger> </i:interaction. triggers> </Button> </Grid> </Window>
You need to note that in the Window tab
Xmlns:i= "Clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity", which corresponds to the background using System ; the ability to load an assembly
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/
In this way, we can handle the events of all the target elements.
Again we face another question, can we bind key events and handle certain key-value commands? The answer is yes, you can use the keybinding, you can also use mousebinding to handle the mouse events.
<textbox text= "Test" >
<TextBox.InputBindings>
<keybinding key= "S" modifiers= "Alt" command= " {Binding Keyeventcommand} "></KeyBinding>
<mousebinding gesture=" RightClick "mouseaction=" Leftclick " command=" {Binding Mouseeventcommand} "></MouseBinding>
</textbox.inputbindings >
</TextBox>
<label content= "Test" >
<Label.InputBindings>
<keybinding key= "S" modifiers= "Alt" command= "{Binding Keyeventcommand}" ></KeyBinding>
<mousebinding mouseaction= "Leftclick" command= "{Binding Mouseeventcommand}" ></MouseBinding>
</ Label.inputbindings>
</Label>
Here we have a inputbindings binding for the TextBox and the label.
It is to be noted that:
1.InputBindings is a binding set inputbindingcollection that can have multiple mousebinding and multiple keybinding
The 2.KeyBinding bound object needs to be focused, and the label here cannot respond to the Alt+s event because it cannot be focused
The priority of gesture in 3.MouseBinding is higher than mouseaction, and when gesture is set, Leftclick will fail