MVVM: Binding a ViewModel which takes constructor args to a UserControl

后端 未结 1 1743
不思量自难忘°
不思量自难忘° 2021-01-21 07:25

My WPF app has a MainWindow containing a usercontrol called TvshowGridView.

MainWindow:



        
相关标签:
1条回答
  • 2021-01-21 07:45

    Assuming that your MainWindows Datacontext is an instance of MainWindowViewModel, you can bind the usercontrol to TvshowGridViewModel like this:

    <Window>
    ... 
    <Grid x:Name="LayoutRoot">
        <views:TvshowGridView DataContext={Binding TvshowGridViewModel} x:Name="TheTvshowGridView" Margin="8,8,8,58.96" Grid.Row="1"/>
    </Grid>
    

    You also should change the TvshowGridViewModel property code like shown:

    public TvshowGridViewModel TvshowGridViewModel 
    { get{return _tvshowGridViewModel;} }
    
    0 讨论(0)
提交回复
热议问题