我将如何在Loaded =“"上传递参数? &quot ;? [英] How would I go about passing a parameter on Loaded=" "?

查看:127
本文介绍了我将如何在Loaded =“"上传递参数? &quot ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在Loaded=" "上传递一个枚举参数,这样我就可以轻松地识别正在加载的部分,而不必在名称上进行字符串欺骗.

I want to be able to pass an enum parameter upon Loaded=" " so I can easily identify the section that is loading without having to do string trickery on the name.

我的Expander XAML:

My Expander XAML:

<Expander Loaded="ExpanderLoaded" x:Name="Greeting_And_Opening_Expander" ExpandDirection="Down" IsExpanded="True" FontSize="14" FontWeight="Bold" Margin="5" BorderThickness="1" BorderBrush="#FF3E3D3D">

我希望它调用的方法:

    private void ExpanderLoaded(object sender, RoutedEventArgs e, Sections section)
    {
        //Do stuff
    }

我的枚举(它将大大增加,这只是一个测试运行):

My Enum (It will be significantly larger, this is just a test run):

public enum Sections
{
    Default = 0,
    Opening = 1,
    Verify = 2
}

如何在加载时将枚举作为参数传递?

How do I go about passing the enum as a parameter upon Loading?

推荐答案

我将使用EventTrigger和InvokeCommand操作来执行此操作,以这种方式在您的视图模型ElementLoaded中调用(由于缺少更好的名称),并传递了适当的枚举内.

I would do this using EventTrigger and InvokeCommand action, that way in your view model ElementLoaded (For lack of a better name) gets called and the appropriate Enumeration gets passed in.

<Expander>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding ElementLoaded}" 
                               CommandParameter="{x:Static local:Sections.Default}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Expander>

在ViewModel中,您将拥有ICommand类型的属性,称为ElementLoaded,然后在构造函数中将其初始化为

In your ViewModel, you will have a property of type ICommand called ElementLoaded, then in your constructor you initialize it as so

ElementLoaded = new ActionCommand(ElementLoadedMethod);

和ElementLoadedMethod可以这样

and the ElementLoadedMethod can be as so

    private void ElementLoadedMethod(object section)
    {
        var sectionEnumVal =  (Sections)section;
    }

这应该是您要做的所有事情.

This should be all you have to do.

这篇关于我将如何在Loaded =“"上传递参数? &quot ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆