简体   繁体   English

我将如何在Loaded =“”上传递参数?

[英]How would I go about passing a parameter on 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. 我希望能够在Loaded=" "上传递一个枚举参数,这样我就可以轻松识别正在加载的部分,而不必对名称进行字符串欺骗。

My Expander XAML: 我的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">

The Method I want it to call: 我要它调用的方法:

    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? 如何在加载时将枚举作为参数传递?

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. 我将使用EventTrigger和InvokeCommand操作执行此操作,以这种方式在视图模型ElementLoaded中(由于缺少更好的名称)被调用,并传递适当的Enumeration。

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

In your ViewModel, you will have a property of type ICommand called ElementLoaded, then in your constructor you initialize it as so 在ViewModel中,您将拥有ICommand类型的属性ElementLoaded,然后在构造函数中将其初始化为

ElementLoaded = new ActionCommand(ElementLoadedMethod);

and the ElementLoadedMethod can be as so 和ElementLoadedMethod可以这样

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

This should be all you have to do. 这应该是您要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM