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

发布于 2024-09-13 01:48:50 字数 82 浏览 8 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提赋 2024-09-20 01:48:50

为了避免视图上的代码隐藏,请使用交互库(System.Windows.Interactivity dll,您可以从 Microsoft 免费下载 - 也随 Expression Blend 一起提供)。

然后您可以创建一个执行命令的行为。这样,触发器调用行为,行为又调用命令。

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

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

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);            
    }
}
往日情怀 2024-09-20 01:48:50
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
       ApplicationCommands.New.Execute(null, targetElement); 
       // or this.CommandBindings[0].Command.Execute(null); 
    }

和 xaml

    Loaded="Window_Loaded"
    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"
萌辣 2024-09-20 01:48:50

AttachedCommandBehavior V2 aka ACB<提出了一种更通用的使用行为的方法< /a> 它甚至支持多个事件到命令的绑定,

这是一个非常基本的使用示例:

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

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"
/>
桃扇骨 2024-09-20 01:48:50

现在这更容易做到。只需包含以下命名空间:

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

并像这样利用它:

<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding CommandInViewModel}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文