我的WPF应用程序有一个MainWindow,其中包含一个名为TvshowGridView的用户控件。
主窗口:
<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>
我希望绑定到TvshowGridView的视图模型TvshowGridViewModel有一个带参数的构造函数。
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();
}
...
}
这些参数在MainWindowViewModel中定义,MainWindowViewModel是连接到MainWindow的视图模型。 - 所以这似乎是创建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; }
...
}
如何将实例化的TvshowGridViewModel绑定到TvshowGridView? (避免代码隐藏)
谢谢!
答案 0 :(得分:3)
假设您的MainWindows Datacontext是MainWindowViewModel的一个实例,您可以将usercontrol绑定到TvshowGridViewModel,如下所示:
<Window>
...
<Grid x:Name="LayoutRoot">
<views:TvshowGridView DataContext={Binding TvshowGridViewModel} x:Name="TheTvshowGridView" Margin="8,8,8,58.96" Grid.Row="1"/>
</Grid>
您还应该更改TvshowGridViewModel属性代码,如下所示:
public TvshowGridViewModel TvshowGridViewModel
{ get{return _tvshowGridViewModel;} }