如何在wpf中加载窗口时触发命令

时间:2022-12-03 19:25:24

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.,)

是否可以触发命令以通知窗口已加载。另外,我没有使用任何MVVM框架(在某种意义上的框架,Caliburn,Onxy,MVVM Toolkit等)

3 个解决方案

#1


18  

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).

要避免View上的代码,请使用Interactivity库(System.Windows.Interactivity dll,您可以从Microsoft免费下载 - 也随Expression Blend一起提供)。

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

然后,您可以创建执行命令的行为。这样,Trigger调用调用Command的Behavior。

<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:

CommandAction(也使用System.Windows.Interactivity)可能如下所示:

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);            
    }
}

#2


7  

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
       ApplicationCommands.New.Execute(null, targetElement); 
       // or this.CommandBindings[0].Command.Execute(null); 
    }

and xaml

和xaml

    Loaded="Window_Loaded"

#3


2  

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

在AttachedCommandBehavior V2又名ACB中提出了一种更通用的使用行为的方法,它甚至支持多个事件到命令的绑定,

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"
/>

#1


18  

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).

要避免View上的代码,请使用Interactivity库(System.Windows.Interactivity dll,您可以从Microsoft免费下载 - 也随Expression Blend一起提供)。

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

然后,您可以创建执行命令的行为。这样,Trigger调用调用Command的Behavior。

<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:

CommandAction(也使用System.Windows.Interactivity)可能如下所示:

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);            
    }
}

#2


7  

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
       ApplicationCommands.New.Execute(null, targetElement); 
       // or this.CommandBindings[0].Command.Execute(null); 
    }

and xaml

和xaml

    Loaded="Window_Loaded"

#3


2  

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

在AttachedCommandBehavior V2又名ACB中提出了一种更通用的使用行为的方法,它甚至支持多个事件到命令的绑定,

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"
/>