Passing Parameters between xaml window and usercontrol WPF

后端 未结 3 1121
别跟我提以往
别跟我提以往 2021-02-14 04:02

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 the

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-14 04:38

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

    
    

    then you can try this in constructor :

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

提交回复
热议问题