我的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>
我希望绑定(bind)到TvshowGridView的 View 模型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(连接到MainWindow的 View 模型)中定义。 -因此,这似乎是创建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绑定(bind)到TvshowGridView? (避免出现代码隐藏)
谢谢!
最佳答案
假设您的MainWindows Datacontext是MainWindowViewModel的实例,可以将用户控件绑定(bind)到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;} }