简体   繁体   English

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

[英]Passing Parameters between xaml window and usercontrol WPF

How to pass Parameters from xaml window to WPF usercontrol constructor? 如何将参数从xaml窗口传递给WPF usercontrol构造函数? 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扩展还是有其他方法可以做到这一点?

<local:Myusercontrol Param1="user1"/>

xaml.cs of the calling Window , well its usercontrol. 调用Window xaml.cs,以及它的用户控件。

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 和usercontrol的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模式。

XAML can't use constructor with parameter. XAML不能使用带参数的构造函数。 Keep what you've tried with dependency property : 使用依赖属性保留您尝试过的内容:

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

You are passing a parameter to user control from XAML which will not work. 您正在从XAML将参数传递给用户控件,这将无法正常工作。 Because if you call your user control from XAML, it is going to call the default constructor of your user control. 因为如果从XAML调用用户控件,它将调用用户控件的默认构造函数。 In your case you haven't created any paramterless constructor. 在你的情况下,你没有创建任何无参数的构造函数。 The other one with parameter Myusercontrol (string User) will never get called. 带参数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. 在那里,您应该为UserControlViewModel创建一个属性并创建其实例。 If you do that it would be easier for you to bind your user control's Datacontext to its ViewModel. 如果这样做,您将更容易将用户控件的Datacontext绑定到其ViewModel。

If your user control will be used at many places, i recommend har07 answer and use dependency property. 如果您的用户控件将在许多地方使用,我建议使用har07答案并使用依赖属性。 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 例如,将网格创建为父级,并将控件作为网格的子级插入

XAML XAML

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

Code

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

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

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