MVVM: Binding a ViewModel which takes constructor

2019-02-25 13:43发布

问题:

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

MainWindow:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:NevermissClient"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:views="clr-namespace:NevermissClient.Views"
    x:Class="NevermissClient.MainWindow"
    x:Name="Window">
    <Grid x:Name="LayoutRoot">
        <views:TvshowGridView x:Name="TheTvshowGridView" Margin="8,8,8,58.96" Grid.Row="1"/>
    </Grid>
</Window>

TvshowGridView:

<UserControl
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:viewModels="clr-namespace:NevermissClient.ViewModels"
     xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
     mc:Ignorable="d"
     x:Class="NevermissClient.Views.TvshowGridView"
     d:DesignWidth="640" d:DesignHeight="480">

    <Grid x:Name="LayoutRoot">

        <telerik:RadGridView x:Name="TvshowGrid" d:LayoutOverrides="Width, Height" AutoGenerateColumns="False"  ItemsSource="{Binding AllEpisodes}" IsReadOnly="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding TvshowName, Mode=TwoWay}" Header="Tvshow Name" IsReadOnly="False"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Name, Mode=TwoWay}" Header="Episode Name"/>

                <telerik:GridViewDataColumn DataMemberBinding="{Binding Airdate, Mode=TwoWay}" Header="Airdate"/>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>

    </Grid>
</UserControl>

The view model, TvshowGridViewModel, that I wish to bind to the TvshowGridView has a constructor that takes arguments.

public class TvshowGridViewModel : BaseViewModel
{
    private EpisodeRepository _episodeRepository;
    private TvshowRepository _tvshowRepository;

    public ObservableCollection<EpisodeViewModel> AllEpisodes { get; private set; }

    public TvshowGridViewModel(EpisodeRepository episodeRepository, TvshowRepository tvshowRepository)
    {
        _episodeRepository = episodeRepository;
        _tvshowRepository = tvshowRepository;

        CreateAllEpisodes();
    }
    ...
}

These arguments are defined in MainWindowViewModel, the view model connected to the MainWindow. - So this seems like the logical place to create the TvshowGridViewModel.

public class MainWindowViewModel : BaseViewModel
{
    readonly TvshowGridViewModel _tvshowGridViewModel;
    readonly EpisodeRepository _episodeRepository;
    readonly TvshowRepository _tvshowRepository;

    public MainWindowViewModel()
    {
        _episodeRepository = new EpisodeRepository("c:\data.xml");
        _tvshowRepository = new TvshowRepository("c:\data.xml");

        _tvshowGridViewModel = new TvshowGridViewModel(_episodeRepository, _tvshowRepository);
    }

    public TvshowGridViewModel TvshowGridViewModel { get; }
    ...
}

How can I bind the instantiated TvshowGridViewModel to the TvshowGridView? (Avoiding codebehind)

Thanks!

回答1:

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