How to fire a Command when a window is loaded in wpf

前端 未结 4 1427
Happy的楠姐
Happy的楠姐 2020-12-14 23:08

Is it possible to fire a command to notify the window is loaded. Also, I\'m not using any MVVM frameworks (Frameworks in the sense, Caliburn, Onxy, MVVM Toolkit etc.,)

相关标签:
4条回答
  • 2020-12-14 23:32

    To avoid code behind on your View, use the Interactivity library (System.Windows.Interactivity dll which you can download for free from Microsoft - also comes with Expression Blend).

    Then you can create a behavior that executes a command. This way the Trigger calls the Behavior which calls the Command.

    <ia:Interaction.Triggers>
        <ia:EventTrigger EventName="Loaded">
            <custombehaviors:CommandAction Command="{Binding ShowMessage}" Parameter="I am loaded"/>
        </ia:EventTrigger>
    </ia:Interaction.Triggers>
    

    CommandAction (also uses System.Windows.Interactivity) can look like:

    public class CommandAction : TriggerAction<UIElement>
    {
        public static DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandAction), null);
        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }
    
    
        public static DependencyProperty ParameterProperty = DependencyProperty.Register("Parameter", typeof(object), typeof(CommandAction), null);
        public object Parameter
        {
            get
            {
                return GetValue(ParameterProperty);
            }
            set
            {
                SetValue(ParameterProperty, value);
    
            }
        }
    
        protected override void Invoke(object parameter)
        {
            Command.Execute(Parameter);            
        }
    }
    
    0 讨论(0)
  • 2020-12-14 23:32
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           ApplicationCommands.New.Execute(null, targetElement); 
           // or this.CommandBindings[0].Command.Execute(null); 
        }
    

    and xaml

        Loaded="Window_Loaded"
    
    0 讨论(0)
  • 2020-12-14 23:34

    A more generic way using behaviors is proposed at AttachedCommandBehavior V2 aka ACB and it even supports multiple event-to-command bindings,

    Here is a very basic example of use:

    <Window x:Class="Example.YourWindow"
            xmlns:local="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
            local:CommandBehavior.Event="Loaded"
            local:CommandBehavior.Command="{Binding DoSomethingWhenWindowIsLoaded}"
            local:CommandBehavior.CommandParameter="Some information"
    />
    
    0 讨论(0)
  • 2020-12-14 23:34

    This is much easier to do now. Simply include the following namespace:

    xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
    

    And leverage it like this:

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding CommandInViewModel}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
    
    0 讨论(0)
提交回复
热议问题