c# - CallMethodAction binding to Button fails -
there no errors. trying learn mvvm. setup simple. no output when clicking on button. xaml should ok since have produced interactions part in blend dragging behavior button. note: intention use methods, not command, since commands cover click, not example doubleclick.
<window x:class="mvvm_1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:i="clr-namespace:system.windows.interactivity;assembly=system.windows.interactivity" xmlns:local="clr-namespace:mvvm_1" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" title="mainwindow" width="525" height="350" mc:ignorable="d" > <window.datacontext> <local:viewmodel/> </window.datacontext> <grid> <button x:name="button" width="75" margin="198,168,0,0" horizontalalignment="left" verticalalignment="top" content="testmethod"> <i:interaction.triggers> <i:eventtrigger eventname="mouseleftbuttondown" sourcename="button"> <ei:callmethodaction methodname="methodtesting" targetobject="{binding}"/> </i:eventtrigger> </i:interaction.triggers> </button> </grid>
using system.windows; namespace mvvm_1 { public class viewmodel { public static void methodtesting() { messagebox.show("success!"); } } }
you need use command bind control click event.
mvvm - commands, relaycommands , eventtocommand
instead of:
public static void methodtesting() { messagebox.show("success!"); }
use this example:
public icommand buttonclickcommand { { return new delegatecommand<object>(functocall, functoevaluate);} } private void functocall(object context) { //this called when button clicked messagebox.show("success!"); } private bool functoevaluate(object context) { //this called evaluate whether functocall can called //for example can return true or false based on validation logic return true; }
also under how create onclick command in wpf mvvm programmatically created button? - is great example.
Comments
Post a Comment