如何将 xaml 标记文件中的参数传递给 UserControl 构造函数? [英] How can I pass a parameter from the xaml markup file to the UserControl constructor?

查看:21
本文介绍了如何将 xaml 标记文件中的参数传递给 UserControl 构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 StepsWnd 窗口,其中 UserControl StepProp 被使用了两次,在一个单独的文件中声明.

I have a StepsWnd window in which UserControl StepProp is used twice, declared in a separate file.

 <Window x:Class="MyProject.StepsWnd"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:c1="http://schemas.componentone.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyProject"
    Height="550" Width="850">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <local:StepProp Grid.Column="0" DataContext="{Binding Path=PrevStepVM}" x:Name="m_PrevStep"/>
        <local:StepProp Grid.Column="1" DataContext="{Binding Path=CurStepVM}" x:Name="m_CurStep"/>
    </Grid>
</Window>

当 StepsWnd 窗口被创建时,StepProp 构造函数被调用两次 - 分别用于 m_PrevStep 和 m_CurStep.

When the StepsWnd window is created, the StepProp constructor is called twice - for m_PrevStep and for m_CurStep.

public class StepProp : UserControl
{
    public StepProp()
    {
        InitializeComponent();
    }
    //...
}

如何将参数从 StepsWnd 窗口的标记传递给 StepProp 类构造函数,以便我可以识别谁在调用构造函数 m_PrevStep 或 m_CurStep?得到这样的东西?

How can I pass a parameter to the StepProp class constructor from the markup of the StepsWnd window, so that I can identify who is calling the constructor, m_PrevStep or m_CurStep? To get something like this?

public class StepProp : UserControl
{
    public StepProp(object parameter)
    {
        InitializeComponent();
        if ((string)parameter == "PrevStep")
        {
            //todo somthing
        }
        else if ((string)Param == "CurStep")
        {
            //todo somthing else
        }
    }
    //...
}

推荐答案

如何从 StepsWnd 窗口的标记中将参数传递给 StepProp 类构造函数,以便识别调用构造函数的人是 m_PrevStep 还是 m_CurStep?

How can I pass a parameter to the StepProp class constructor from the markup of the StepsWnd window, so that I can identify who is calling the constructor, m_PrevStep or m_CurStep?

你不能.XAML 是一种 标记 语言,您不能使用任何其他构造函数来创建 UserControl 的实例,而不是使用不接受任何参数的默认构造函数.所以忘记在 XAML 中使用依赖注入吧.

You can't. XAML is a markup language and you cannot create an instance of a UserControl using any other constructor than the default one that doesn't accept any arguments. So forget about using dependency injection in XAML.

如果您希望构造函数根据您创建的实例而表现出不同的行为,您可能应该考虑创建两种不同的 UserControl 类型,例如,它们可能共享相同的基类或相互继承.

If you want the constructor to behave differently depending on which instance you are creating, you should probably consider creating two different UserControl types that for example may share the same base class or inheriting from one another.

或者,您可以按照评论中的建议定义和设置属性:

Alternatively you could define and set a property as suggested in the comments:

<local:StepProp x:Name="m_PrevStep" YourProperty="m_PrevStep" />

...并处理 setter 或属性中的任何逻辑(如果您正在定义依赖属性,则为回调).

...and handle any logic in the setter or the property (or the callback if you are defining a dependency propery).

请注意,属性值在构造函数中不可用,因为必须在 XAML 处理程序实际设置属性之前创建实例.

Note that the property value won't be available in the constructor though as the instance must be created before the XAML processer can actually set the property.

这篇关于如何将 xaml 标记文件中的参数传递给 UserControl 构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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