在xaml窗口和usercontrol WPF之间传递参数

时间:2021-05-28 00:03:20

How to pass Parameters from xaml window to WPF usercontrol constructor? I have tried creating dependency property, but it fails to do it. Should I try xaml extensions or is there any other way to do it?

如何将参数从xaml窗口传递给WPF usercontrol构造函数?我尝试过创建依赖属性,但它没有这么做。我应该尝试xaml扩展,还是有其他方法?

<local:Myusercontrol Param1="user1"/>

xaml.cs of the calling Window, well its usercontrol.

xaml。调用窗口的cs,它的usercontrol。

public partial class SomeView : UserControl
{
    SomeViewModel vm = new SomeViewModel();
    public SomeView()
    {
        this.DataContext = vm;
        InitializeComponent;
    }
}

InitializeComponent of above window clears value of dependency property set through xaml before creating an instance of the user control and hence value of depencency property is always null.

在创建用户控件的实例之前,上面窗口的InitializeComponent清除通过xaml设置的依赖属性的值,因此depencency属性的值总是null。

and usercontrol's xaml.cs

和用户控件的xaml.cs

Myusercontrol : UserControl
{
  public Myusercontrol (string User)
  {
    InitializeComponent();
    UserControlViewModel vm = new UserControlViewModel (User);
    this.DataContext = vm;
  }

Note that I am using MVVM pattern.

注意,我使用的是MVVM模式。

3 个解决方案

#1


8  

XAML can't use constructor with parameter. Keep what you've tried with dependency property :

XAML不能使用带参数的构造函数。保留你尝试过的依赖属性:

<local:Myusercontrol Param1="user1"/>

then you can try this in constructor :

然后你可以在构造函数中尝试:

public Myusercontrol()
{
    InitializeComponent();
    Loaded += (sender, args) =>
          {
                UserControlViewModel vm = new UserControlViewModel(Param1);
                this.DataContext = vm;
          };
}

#2


2  

You are passing a parameter to user control from XAML which will not work. Because if you call your user control from XAML, it is going to call the default constructor of your user control. In your case you haven't created any paramterless constructor. The other one with parameter Myusercontrol (string User) will never get called.

您正在从XAML向用户控件传递一个参数,这个参数将不起作用。因为如果您从XAML调用您的用户控件,它将调用您的用户控件的默认构造函数。在您的例子中,您没有创建任何无参数的构造函数。另一个参数为Myusercontrol (string User)的不会被调用。

The best option is to have only the paramterless constructor for your User Control and nothing more.

最好的选择是只有用户控件的无参数构造函数,仅此而已。

public Myusercontrol()
{
    InitializeComponent();
}

Since you are using MVVM for your user control, you should be using MVVM for the window which hosts the user control - as simple as that :)

由于用户控件使用MVVM,所以应该将MVVM用于承载用户控件的窗口——很简单:)

There you should create a property for UserControlViewModel and create its instance. If you do that it would be easier for you to bind your user control's Datacontext to its ViewModel.

在那里,您应该为UserControlViewModel创建一个属性并创建它的实例。如果您这样做,那么将用户控件的Datacontext绑定到它的ViewModel将更容易。

#3


0  

If your user control will be used at many places, i recommend har07 answer and use dependency property. But if your control will be used just once or two times and you're lazy enough to create a dependency property, you can always create your control from code. For example create a grid as parent and insert your control as child of your grid

如果您的用户控件将在许多地方使用,我推荐har07回答并使用依赖属性。但是,如果您的控件只使用一次或两次,并且您足够懒,可以创建一个依赖项属性,那么您可以始终从代码创建控件。例如,创建一个网格作为父窗体,并将控件作为网格的子节点插入。

XAML

XAML

<Grid Name="Wrapper"></Grid>

Code

代码

MyUserControl userControl = new MyUserControl(param);
Wrapper.Children.Add(userControl);

#1


8  

XAML can't use constructor with parameter. Keep what you've tried with dependency property :

XAML不能使用带参数的构造函数。保留你尝试过的依赖属性:

<local:Myusercontrol Param1="user1"/>

then you can try this in constructor :

然后你可以在构造函数中尝试:

public Myusercontrol()
{
    InitializeComponent();
    Loaded += (sender, args) =>
          {
                UserControlViewModel vm = new UserControlViewModel(Param1);
                this.DataContext = vm;
          };
}

#2


2  

You are passing a parameter to user control from XAML which will not work. Because if you call your user control from XAML, it is going to call the default constructor of your user control. In your case you haven't created any paramterless constructor. The other one with parameter Myusercontrol (string User) will never get called.

您正在从XAML向用户控件传递一个参数,这个参数将不起作用。因为如果您从XAML调用您的用户控件,它将调用您的用户控件的默认构造函数。在您的例子中,您没有创建任何无参数的构造函数。另一个参数为Myusercontrol (string User)的不会被调用。

The best option is to have only the paramterless constructor for your User Control and nothing more.

最好的选择是只有用户控件的无参数构造函数,仅此而已。

public Myusercontrol()
{
    InitializeComponent();
}

Since you are using MVVM for your user control, you should be using MVVM for the window which hosts the user control - as simple as that :)

由于用户控件使用MVVM,所以应该将MVVM用于承载用户控件的窗口——很简单:)

There you should create a property for UserControlViewModel and create its instance. If you do that it would be easier for you to bind your user control's Datacontext to its ViewModel.

在那里,您应该为UserControlViewModel创建一个属性并创建它的实例。如果您这样做,那么将用户控件的Datacontext绑定到它的ViewModel将更容易。

#3


0  

If your user control will be used at many places, i recommend har07 answer and use dependency property. But if your control will be used just once or two times and you're lazy enough to create a dependency property, you can always create your control from code. For example create a grid as parent and insert your control as child of your grid

如果您的用户控件将在许多地方使用,我推荐har07回答并使用依赖属性。但是,如果您的控件只使用一次或两次,并且您足够懒,可以创建一个依赖项属性,那么您可以始终从代码创建控件。例如,创建一个网格作为父窗体,并将控件作为网格的子节点插入。

XAML

XAML

<Grid Name="Wrapper"></Grid>

Code

代码

MyUserControl userControl = new MyUserControl(param);
Wrapper.Children.Add(userControl);