How to handle event in MVVM ?

Hi everyone ,
I'm currently learning to use MVVM model , about command handling, it's ok for me now. But , about Event handling, it causes me some difficulties. 
Can anyone help me to solve this problem : I have a window and I want to handle the its Closing event . For example, when the window is closing , if the bool chxAllowToClose.IsChecked = true , the window can be closed, vice versa, the window will display a
warning message : "Please check the checkbox" and then prevent the close operation 
Thank you

Windows can be closed in a number of ways and the simplest way to trap them would be to handle Window Closing in code behind.
This is also the easiest way to show dialogs if that's how you wanted the warning message to appear.
This:
chxAllowToClose.IsChecked
Looks like a checkbox to me, seeing as you have IsChecked.
That presumably means you already have that bound to a viewmodel and visible to the user.
You can therefore just test that value from the view.
If you prefer to hide that instead then there are several options.
You could have a dependency property and a binding in code behind which binds to the viewmodel. Test that in your event.
Or
You could save the value when it changes to application.current.resources.  Since dependency properties and bindings are a bit wordy this is easier.
Or
You could have just a variable in the view code behind and use messenger to tell that when the value changes from the viewmodel:
http://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between-classes.aspx
Please don't forget to upvote posts which you like and mark those which answer your question.
My latest Technet article - Dynamic XAML

Similar Messages

  • How to handle Events in OO abap.

    Hii all as im new to SAP and  ABAP , i want to know , what are events all about and how to handle events?
    Like how to guide double click to call a transaction , or to create a hot sopt and then , calling the transaction...........
    Please Help.

    Hi Chandan,
    possibly you are not just new to ABAP but new to obeject oriented programming in general. In oo, you use events to trigger methods that are registered as 'listeners' for the event.
    As I have no idea on your current knowledge, I don't know what I could recommend to start with.
    My personal approach is first to have a task I want to complete and then find and understand the methods to get there.
    Regards,
    Clemens

  • How to handle events in webdynpro abap

    Hi,
    can any body explain how to handle the events in webdynpro abap.
    i want to know some concepts in general.
    Thanks,

    Hi Mahesh,
    you can create event handlers under the actions tab in you view. evry event handler has an importing parameter wdevent of type ref to cl_wd_custom_event.
    you can also create events in your component controller and they can be handled within your views
    check cl_wd_custom_event class for details about what all information you get when an event occurs.
    for further details you can check out the following links
    http://help.sap.com/saphelp_nw04s/helpdata/en/eb/ed6f4169e25858e10000000a1550b0/frameset.htm
    http://help.sap.com/saphelp_nw04s/helpdata/en/a9/c751415e3b6532e10000000a1550b0/frameset.htm
    also you can try the tutorial at the following link for further clarity
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/2eb11b59-0a01-0010-dfa3-8292abdf9c4f
    Regards,
    Shweta
    Message was edited by:
            Shweta R Shanbhag

  • How to handle Event in JACOB API

    Hi,
    I am read the outlook mail using JACOB[Java Com Bridge] API successfully, now i want to handle events for outlook mail using JACOB. and also i can able to catch the new mail event. but i am unable to catch any deleted mail and move mail from one folder to another folder. This is extremely important for my project. so any one please tell me how to handle and catch the Outlook deleted event and move event using JACOB.
    Thanks in Advance,
    With Regards,
    Ganesh

    Windows can be closed in a number of ways and the simplest way to trap them would be to handle Window Closing in code behind.
    This is also the easiest way to show dialogs if that's how you wanted the warning message to appear.
    This:
    chxAllowToClose.IsChecked
    Looks like a checkbox to me, seeing as you have IsChecked.
    That presumably means you already have that bound to a viewmodel and visible to the user.
    You can therefore just test that value from the view.
    If you prefer to hide that instead then there are several options.
    You could have a dependency property and a binding in code behind which binds to the viewmodel. Test that in your event.
    Or
    You could save the value when it changes to application.current.resources.  Since dependency properties and bindings are a bit wordy this is easier.
    Or
    You could have just a variable in the view code behind and use messenger to tell that when the value changes from the viewmodel:
    http://social.technet.microsoft.com/wiki/contents/articles/26070.communicating-between-classes.aspx
    Please don't forget to upvote posts which you like and mark those which answer your question.
    My latest Technet article - Dynamic XAML

  • How to handle event when the user closing the browser (Urgent Please)

    Hi,
    How to handle the event when user closes the browser....
    i want to display some alter message when user trying to close the browser...
    Please can any one help me how i have to do this...........
    Thanks.

    Finally got this working. You cannot use the stop() or destroy methods. By the time they are called all database connections are gone and you will get a null pointer exception.
    You will have to use the onBeforeUnload method in the html file that calls the applet and use JavaScript to call the save method in java which saves the document:
    <SCRIPT LANGUAGE="JScript" TYPE="text/javascript">
    function Save()
    //i call the applets doSave() method from here in which i save all
    //changes to the database
    top.Tree.document.TestApplet.doSave();
    //this will invoke the default IE message for closing the window
    //when user clicks on the x in the browser
    message = "Your document has been saved."
    return message;
    window.onbeforeunload=Save;
    </SCRIPT>
    //the applets doSave()
    public void doSave()
    //this frame provides user with the message that document is being
    //saved
    final JFrame frame = new JFrame("Saving");
    JPanel contentPane = new JPanel();
    JLabel label = new JLabel(" Please wait, saving document...");
    frame.getContentPane().add(label,BorderLayout.CENTER);
    frame.setSize(250, 100);
    frame.setLocation(300, 400);
    frame.setResizable(false);
    frame.setVisible(true);
    frame.addWindowListener(new WindowAdapter()
    //the frame is just for user's information, so prevent user from
    //closing it or iconifying it.
    public void windowClosing(WindowEvent e)
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    public void windowIconified(WindowEvent e)
    frame.setState(frame.NORMAL);
    //call my saveDocument() method that saves everything to the database.
    editorModule.saveDocument();
    //System.out.println("finished calling saving document");
    //once the saving is done the frame with the message disappears
    frame.setVisible(false);
    //System.out.println("setting frame to false");
    //I also had an exit button in my applet, that would perform the same task, however now with the above onBeforeUnload method, the exit message would appear twice, so had to modify my exit button action performed, so that if the exitbutton was clicked the onbeforeunload method would get passed a null value and not do anything.
    private void exitButton_actionPerformed()
    if (DEBUG) System.out.println("Calling exit");
    doSave();
    window.eval("this.onbeforeunload=null;");
    window.eval("top.close();");
    //continue from here if user cancels the closing of the window
    window.eval("this.onbeforeunload=doSave;");
    window.eval("top.focus()");
    window.eval("document.TestApplet.requestFocus()");
    homePanel.requestFocus();
    }

  • How to handle events in Swng

    Hi!
    I would like to know which one of the following is the best way to handle events in Swing application.
    Method 1
    Write annonymus inner classes in the same class
    Method 2
    =======
    Write a seperate class which extends the adapter class of the event handling and create an object of that in the main class and assign it to the components with addActionHandler() method.
    I am trying to use the second one and I have the following design issue.
    I have a class frmMain.java in which I have a frame and to that frame I am adding a panel which consists of 'N' No. of components.
    I want to make this panel added to the frame when I click on a menu item (login) and want to remove the panel from frame when I click on a menu item(logout).
    I have a main class called Application.java where I create the object of my frame(frmMain.java).
    Thanks in advance,
    AV

    1. Your JFrame is now subject to receive action events from anywhere. You will have to be more careful that you respond only to the right events.
    2. If you have a lot of possible consequences to an event(for example, based on button pressed), you'll need a long if...then...else statement to determine what to do based on the source of the event.
    3. With individual ActionListener classes, it's easier to add the same listener to multiple components and no need to worry about source.
    4. Kind of the same thing: With individual classes, the event and its consequences are so tightly coupled.
    End preaching....basically, my style boils down to what I call the tool set vs Swiss army knife rule. Java seems designed around the concept of a large number of specific purpose classes vs a smaller number of multi purpose classes and I think its a design methodology that makes sense, because I believe strongly in functional isolation in my code.

  • How to handle events between two custom components?

    Hi ,
         i want to handle events between two custom components, example if an event is generated in one custom component ,and i want to handle it any where in the application.....can any one suggest me any tutorial or meterial in this concept...
    thanks

    Events don't really go sideways in ActionScript by default. They bubble upward. If you want to send an event sideways, you will probably have to coordinate with something higher up in the event hierarchy. You can send the event to a common ancestor, and then pass it down the hierarchy as a method argument.
    Another option is to use a framework that supports Injection. There are a number around these days. The one I'm most familiar with is Mate, which allows you to handle events and inject data in MXML. Mate is meant to be used as an MVC framework, and you may want to look into it if your application is complex, but you can also use it to coordinate global event handling if you don't need that level of structure.

  • How to handle events when artboard list change in Illustrator?

    It is very strange for me, but AIEvent has no event type for handle artboard list change events.
    http://cssdk.host.adobe.com/sdk/1.5/docs/WebHelp/references/cshalib/com/adobe/cshostadapte r/AIEvent.html
    May be there are any ideas how to get artboard events: "add", "delete", "reorder" 

    Well, the right event type is AIEvent.DOCUMENT_CROP_AREA_MODIFIED. It is very usefull to play with EventWatcher example from Remote Creative Suite SDK Examples.

  • How to handle event structures for two buttons and two counters.

         *I have two buttons A and B, and have to compare the difference in hits between them.  So do I place an event structure within a while loop for each button (2 buttons, 2 event structures, 2 while loops), or does it need to be one event structure with cases for both buttons whithin one while loop? 
         *How do I pass the values of buttons A and B's hit counters out of the while loop in order to calculate the differences between them after each increment, not just the final count after the stop button is hit? 
         *How would I make one stop button work to handle both buttons A and B?   I tried to place the actual stop button in one button A's structure and its global in B's structure, but It wont seem to let me use a local variable for a latchable control
    Checker

    You should probably have not started a new thread and waited until your question in the original thread was answered. You can modify altenbach's example for a second button. You have a single event structure with a new event for the second button. You have a second shift register to track the number of times the second button is pressed. Inside the while loop but outside the event structure, you just have a subtract function in or to report the difference. do not modify the posted example for the stop button. It works perfectly as is.

  • How to handle events in J Table (Enter event)

    I created a J Table having multiple rows and columns. I want to have the event to happen on second column, on entering the code on first column, and then pressing ENTER.
    Eg:
    first column i enter code : say A010
    In the code master, the description matching the above code is "HIGH TENSION CABLE".
    When I enter "A010" in the first column and press "ENTER", i want the description to appear in the second column i.e. HIGH TENSION CABLE (by referring the code master, which I already created).
    How this can be handled. Please help me with the code/suggestions. As this urgently required for me in a project, I request the FORUM to help at the earliest.
    Thanking you in advance.
    PS: I shall appreciate if the reply is also sent to my email id: "[email protected]".
    N Murali

    This code would do your work
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class TestTable
        private JTable table;
        private AbstractTableModel model;
        private DefaultTableModel defaultModel;
        private JScrollPane jsp;
        public TestTable()
            table = new JTable();
            defaultModel = new DefaultTableModel(10,5);
            setModel(defaultModel);
            jsp = new JScrollPane(table);
            hookSetTextOnEnter();
        protected void hookSetTextOnEnter()
            String actionName = "selectNextRowCell";
            final Action enterAction = table.getActionMap().get(actionName);
            Action myAction = new AbstractAction()
                public void actionPerformed(ActionEvent e)
                    int row = table.getSelectedRow();
                    int column = table.getSelectedColumn();
                    System.out.println(" SELECTING NEXT ROW "+row+""+column);
                    if(row != -1 && column == 2 )
                        Object value = table.getValueAt(row,column);
                        if(value != null && value.toString().equals("A010"))
                        table.setValueAt("HIGH TENSION CABEL",row,column+1);
                    enterAction.actionPerformed(e);
            table.getActionMap().put(actionName,myAction);
        public void setModel(TableModel model)
            table.setModel(model);
        public JScrollPane getScrollPane()
            return jsp;
        public JTable getTable()
            return table;
        public static void main(String[] args)
            TestTable tt = new TestTable();
            JFrame frame = new JFrame("Test Table");
            frame.getContentPane().add(tt.getScrollPane());
            frame.pack();
            frame.setVisible(true);
    }------- Unformatted Version------------
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.table.*;
    public class TestTable
    private JTable table;
    private AbstractTableModel model;
    private DefaultTableModel defaultModel;
    private JScrollPane jsp;
    public TestTable()
    table = new JTable();
    defaultModel = new DefaultTableModel(10,5);
    setModel(defaultModel);
    jsp = new JScrollPane(table);
    hookSetTextOnEnter();
    protected void hookSetTextOnEnter()
    String actionName = "selectNextRowCell";
    final Action enterAction = table.getActionMap().get(actionName);
    Action myAction = new AbstractAction()
    public void actionPerformed(ActionEvent e)
    int row = table.getSelectedRow();
    int column = table.getSelectedColumn();
    System.out.println(" SELECTING NEXT ROW "+row+""+column);
    if(row != -1 && column == 2 )
    Object value = table.getValueAt(row,column);
    if(value != null && value.toString().equals("A010"))
    table.setValueAt("HIGH TENSION CABEL",row,column+1);
    enterAction.actionPerformed(e);
    table.getActionMap().put(actionName,myAction);
    public void setModel(TableModel model)
    table.setModel(model);
    public JScrollPane getScrollPane()
    return jsp;
    public JTable getTable()
    return table;
    public static void main(String[] args)
    TestTable tt = new TestTable();
    JFrame frame = new JFrame("Test Table");
    frame.getContentPane().add(tt.getScrollPane());
    frame.pack();
    frame.setVisible(true);
    Thanks,
    Kalyan

  • How to handle events occuring on different items in a tree control

    I am developing a small application in which I need to display different panels when user clicks on a tree control item....how to make it display a panel inside a parent panel when a user clicks an item in a tree control....any help would be greatly appreciated. thank you
    If you are young work to Learn, not to earn.

    The tree control can handle several events that you can rely on to handle user interaction. I suggest you take a llok at treeevent.prj example that comes with CVI. In your situation, one possible event to handle is EVENT_SELECTION_CHANGE, that marks the operation the user does when clicking on a new tree item.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • How to Handle Events for JOptionPane buttons?

    Hi All,
    I need a help...
    I am trying to develop a simple SWING application that consists of multiple elements (mainly text fields) and a Submit button. On pressing Submit button, it should validate all the fields and then do something else. But if some fields are left blank, then it should give a pop-up message and on clicking the OK button on the pop-up message, the focus should go to that text field which is left null.
    The problem is I am not able to understand how to Capture this OK button click-event of the pop-up message. I mean how to do something when the OK/Cancle button of the pop-up window is clicked.
    Please Help me.
    Thanks in Advance,
    Ujjal

    As already pointed out, JOptionPane has some static methods which show a dialog, and return an int, or a string. The whole point of this class is to remove the need for any event handling by the developer, that is, you simply call the showDialog method or whatever, and it returns a value telling you, for example, which button was pressed - no need for action listeners at all. The result of the showDialog method (or whichever you use) tells you which button was pressed. Check the javadocs for JOptionPane for details of several constants that indicate which button was pressed
    The class exists to make simple dialogs trivial to generate - you're overcomplicating things!

  • How jslider  handle event?

    Hi evry body, i am writting a code to display a jslider, my question is how to make the jslider handle right arrow key and left arrow key event without using KeyListener?

    Thank you for reply, i made jslider work with keylistener. But i have example that work without keylistener and in my code it doesn't, i don't understand what happens.

  • How to handle event from subVI in the main VI?

    Hello,
    I'm doing some measurement. During the measurement I want the user to see some activity dialog - that's easy to do. Before the measurement starts I dynamically run VI (EX_Progress.vi) that shows some activity and then I do the measurement (in my example simulated by random number generation) . After the measurement is done (or error occurs) I can dynamically abort the activity indicator. But on the other hand I want to give to the user chance to abort the measurement manually via the activity dialog by STOP button. There are some ways how to do it via global variable and check every iteration in the main VI it's state but isn't there some better way? Would be nice to run event in the main VI when the user pushes the STOP button in the Progress VI. Is it possible to achieve this using register events? I tried but don't really now how I'm not familiar with this technique.
    Thanks in advance
    Message Edited by ceties on 11-26-2007 01:34 PM
    LV 2011, Win7
    Attachments:
    Ex.zip ‏374 KB

    OK, here's an example using User Events. It doesn't directly have the main VI listen for the Button press event in the subVI. Instead, it has the main VI listen for a custom User Event that the subVI fires every time there's a button press. You could theoretically directly listen to the button press directly from the Main VI, but you would have to somehow get that control's reference to the main VI. That would require storing a copy of it in a global or some such method. This method I will show here is very common and is definitely worth learning.
    For more info on User Events, refer to the LabVIEW help. You can learn more by clicking any of the User Event VIs and selecting Help from the shortcut menu.
    (I didn't set the subVIs front panel to open automatically, so you'll have to do that yourself to see anything from this demo...)
    Message Edited by Jarrod S. on 11-26-2007 02:51 PM
    Jarrod S.
    National Instruments
    Attachments:
    Example.zip ‏25 KB

  • How to handle events on standard toolbar

    hi, the following code i worte is to load a form and fill data from db table ..but i'm getting only one record and the buttons on standard toolbar like next, previous, first and last are not enabled and not working...what should i do? plz give me the code....
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load       
            Dim SboGuiApi As SAPbouiCOM.SboGuiApi
            Dim sConnectionString As String
            Dim SBO_Application As SAPbouiCOM.Application
            Dim oDICompany As SAPbobsCOM.Company
            Dim ret As Long
            SboGuiApi = New SAPbouiCOM.SboGuiApi
            sConnectionString = Environment.GetCommandLineArgs.GetValue(1)
            sConnectionString = Command()
            SboGuiApi.Connect(sConnectionString)
            SBO_Application = SboGuiApi.GetApplication
            oDICompany = New SAPbobsCOM.Company
            Dim sCookie As String
            sCookie = oDICompany.GetContextCookie()
            Dim conStr As String
            conStr = SBO_Application.Company.GetConnectionContext(sCookie)
            ret = oDICompany.SetSboLoginContext(conStr)
            If Not ret = 0 Then
                Exit Sub 'the operation has failed.           
            End If
            ret = oDICompany.Connect()
            If ret <> 0 Then
                SBO_Application.MessageBox("Failed")
            Else
                SBO_Application.MessageBox("Connected to Database")
            End If
            ' loading(Form)
            Dim oForm As SAPbouiCOM.Form
            Dim creationPackage As SAPbouiCOM.FormCreationParams
            Dim oxmldoc As New Xml.XmlDocument 'u2026when using .netu2019s system.xml
            'create the formcreationparams object
            creationPackage = SBO_Application.CreateObject( _
             SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)
            'please note: these parameters override corresponding data in the xml
            creationPackage.UniqueID = "Sales 111"
            creationPackage.FormType = "Sales Order"
            creationPackage.BorderStyle = SAPbouiCOM.BoFormBorderStyle.fbs_Sizable
            'just a sample for an xml string describing a formu2026 same as used for loadbatchactions
            oxmldoc.Load("c:program filessapsap business oneSales Order.srf")
            creationPackage.XmlData = oxmldoc.InnerXml
            'add the form to the sbo application
            oForm = SBO_Application.Forms.AddEx(creationPackage)
            'set the form title and set it visible!
            oForm.Visible = True
            ''''''''''''''''''''''''''''''Data Binding
            '''''''''''binding data from DBDataSource to form items
            Dim oDBDataSource As SAPbouiCOM.DBDataSource
            Dim oItem As SAPbouiCOM.Item
            Dim oEdit As SAPbouiCOM.EditText
            '''''1st edit box
            oItem = oForm.Items.Item("4")
            oEdit = oItem.Specific
            oForm.DataSources.DBDataSources.Add("AACT")
            oEdit.DataBind.SetBound(True, "AACT", "AcctCode")
            ' getting the data sources bound to the form
            oDBDataSource = oForm.DataSources.DBDataSources.Item("AACT")
            oDBDataSource.Query()
            '''''2nd edit box
            oItem = oForm.Items.Item("5")
            oEdit = oItem.Specific
            oForm.DataSources.DBDataSources.Add("AACT")
            oEdit.DataBind.SetBound(True, "AACT", "AcctName")
            ' getting the data sources bound to the form
            oDBDataSource = oForm.DataSources.DBDataSources.Item("AACT")
            oDBDataSource.Query()
            ''''3rd edit box
            oItem = oForm.Items.Item("6")
            oEdit = oItem.Specific
            oForm.DataSources.DBDataSources.Add("AACT")
            oEdit.DataBind.SetBound(True, "AACT", "CurrTotal")
            ' getting the data sources bound to the form
            oDBDataSource = oForm.DataSources.DBDataSources.Item("AACT")
            oDBDataSource.Query()
        End Sub

    You have to enable the menus on toolbar as
    form.EnableMenu(1288, True)
                 form.EnableMenu(1289, True)
                 form.EnableMenu(1290, True)
                 form.EnableMenu(1291, True)
    If zou have the datas as UDO, use
    form.DataBrowser.BrowseBy = "code" - specifies the logic for getting next, previous,... record
    dont forget to take
    ObjectType="your udo name"
    to the form for specify from which UDO will be the datas.
    If you dont have udo, you need enable themenus in toolbar and in menuevent catch the events and create own logic for this.

Maybe you are looking for

  • How to upload a calculated value

    I am a beginner Dreamweaver CS4 user and I am building a website with PHP and MySQL. My issue will be probably easily solved by any experienced DW user but I couldn't find a solution in tutorials and specialized books. I have to sell a product and I

  • Premiere Elements 11 + Windows 8 pro = slow timeline scrolling!

    Hi! I experience slow redraw of the timeline clips when I scroll, which makes it very difficult, slow and unpleasant to edit! This in nothing new to you all, as I have read in many posts about this problem! And everyone is asking (below these posts)

  • Can not publish

    I try to publish my web by iWeb to Web.me.com got a message: There is error updating mobileme, so who can tell me what can I do

  • Photomerge Faces and Groups

    I use Photoshop CS3 and have recently seen Photoshop Elements which has a number of features such as Photomerge Faces and Photomerte groups. These don't seem to exist in the full version of Photoshop. Is this true or is there a way to add these plugi

  • HT4519 Changing "Reply To" address on Ipad

    I have gmail as my incoming server I have pobox as my outgoing server I have set up my Ipad properly so all outgoing emails run through pobox server.  The problem is the emails have the gmail email in the header.  Is there anyway to change the "reply