How Can I bind DataContext to a Generic ViewModel in XAML?

前端 未结 4 1857
夕颜
夕颜 2021-02-20 09:17

Suppose we have a generic View model like this:

public class MyViewModel : INotifyPropertyChanged where T : Class1
{
    private T _objectModel;
    pub         


        
4条回答
  •  甜味超标
    2021-02-20 09:59

    You're not capable of setting a generic viewmodel in XAML because XAML requires known types at compile time.

    Dependency injection is your best bet

    public class MyControl : UserControl{     
       public MyControl(Object viewModel){    
          this.DataContext = viewModel;
       }
    }
    

提交回复
热议问题