none
How to set EventTrigger in code behinde? RRS feed

  • Question

  • Hi all,

    I have a usercontrol, in which, I create a button by c# code. I want to also add a style with EventTrigger to change a property of this usercontrol, notify this usercontrol to do something. Since I just want to use MVVM to implement it.

    so, my question is that how automatically create a button and use EventTrigger for the click to do something in code behind?

    thanks,

    Joey

    Monday, October 27, 2014 9:35 AM

Answers

  • If you are creating the Button programmatically and want to do something in the code-behind when it is clicked you could simply hook up an Click event handler for it:

    Button button = new Button();
          button.Content = "Click me...";
          button.Click += button_Click;
    
    
        private void button_Click(object sender, RoutedEventArgs e) {
          //handle click...
        }
    

    An EventTrigger is defined within a Style when you want to perform some TriggerAction, typically an animation, when some routed event occur. It is not what you need here though.

    Please refer to my blog post for more information about how to deal with commands in MVVM: http://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/

    The Button also has a Command property which you can bind to a source property of a view model after you have created the Button element:

          Button button = new Button();
          button.Content = "Click me...";
          button.SetBinding(Button.CommandProperty, new Binding("YourViewModelCommandProperty"));
    

    Please remember to mark helpful posts as answer and/or helpful.
    Monday, October 27, 2014 11:52 AM

All replies

  • For a button click, in mvvm you should bind a command.

            <Button  
                Command="{Binding ShowError}"
    ....
            </Button>

    And I suggest you look into MVVM Light ( available via nuget) which has relaycommand, which makes it easier to do commands in MVVM.

    But most of your description raises loads of questions in my mind.
    Why are you programmatically adding a button inside a usercontrol, as opposed to say hiding and showing one you define in markup there?

    How many of these buttons are going to be added to what and how?

    If I give you a style which sets the command of any button to some command or other then how are you going to work out which is what if there are a number?

    If there's just one then why are you creating it in code?

    Monday, October 27, 2014 10:15 AM
    Moderator
  • If you are creating the Button programmatically and want to do something in the code-behind when it is clicked you could simply hook up an Click event handler for it:

    Button button = new Button();
          button.Content = "Click me...";
          button.Click += button_Click;
    
    
        private void button_Click(object sender, RoutedEventArgs e) {
          //handle click...
        }
    

    An EventTrigger is defined within a Style when you want to perform some TriggerAction, typically an animation, when some routed event occur. It is not what you need here though.

    Please refer to my blog post for more information about how to deal with commands in MVVM: http://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/

    The Button also has a Command property which you can bind to a source property of a view model after you have created the Button element:

          Button button = new Button();
          button.Content = "Click me...";
          button.SetBinding(Button.CommandProperty, new Binding("YourViewModelCommandProperty"));
    

    Please remember to mark helpful posts as answer and/or helpful.
    Monday, October 27, 2014 11:52 AM