gpt4 book ai didi

wpf - MVVM:绑定(bind)一个将构造函数参数传递给 UserControl 的 ViewModel

转载 作者:行者123 更新时间:2023-12-04 20:48:42 25 4
gpt4 key购买 nike

我的 WPF 应用有一个主窗口,其中包含一个名为 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>

电视节目 GridView :

<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 中定义,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;} }

关于wpf - MVVM:绑定(bind)一个将构造函数参数传递给 UserControl 的 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4359861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com