在使用Prism的MVVM应用程序中,每次选择TabItem时如何运行方法

尽管有种感觉,这很容易,但我一直在尝试实现这一点,但到目前为止还没有做到。


困难来自以下事实:我已经使用MVVM模式实现了WPF应用程序。现在,这是我对模式和框架的第一次尝试,因此几乎可以保证在尝试遵循MVVM准则时我犯了错误。


我的实施


我有三个视图及其各自的ViewModels(使用PrismAutoWireViewModel方法连接)。的MainView具有TabControl带有两个TabItems,各女巫的包含Frame与所述容器Source设置到其它两个中的一个View第 以下代码是的摘录MainView:


<TabControl Grid.Row="1" Grid.Column="1">

    <TabItem Header="Test">

        <!--TestView-->

        <Frame Source="View1.xaml"/>

    </TabItem>

    <TabItem Header="Results">

        <!--ResultsView-->

        <Frame Source="View2.xaml"/>

    </TabItem>

</TabControl>

我的问题


每当有人更改为特定的东西时TabItem,我都想运行一种方法来更新其中包含的WPF控件之一View。该方法已经实现并绑定到Button,但是理想情况下,不需要任何按钮,我希望可以使用某种方法Event来实现此目的。


我先感谢所有帮助。


GCT1015
浏览 296回答 3
3回答

月关宝盒

这就是我满足这种要求的方式:视图:&nbsp; &nbsp; <Window.DataContext>&nbsp; &nbsp; &nbsp; &nbsp; <local:MainWIndowViewModel/>&nbsp; &nbsp; </Window.DataContext>&nbsp; &nbsp; <Grid>&nbsp; &nbsp; &nbsp; &nbsp; <TabControl Name="tc" ItemsSource="{Binding vms}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TabControl.Resources>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate DataType="{x:Type local:uc1vm}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <local:UserControl1/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <DataTemplate DataType="{x:Type local:uc2vm}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <local:UserControl2/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </DataTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TabControl.Resources>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TabControl.ItemContainerStyle>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Style TargetType="TabItem">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Header" Value="{Binding TabHeading}"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </TabControl.ItemContainerStyle>&nbsp; &nbsp; &nbsp; &nbsp; </TabControl>&nbsp; &nbsp; </Grid></Window>当它具有uc1vm时,它将被模板化到视图中的usercontrol1中。我将绑定到全部实现一个接口的视图模型的集合,因此我确定可以将其强制转换并调用方法。窗口的主要视图模型:&nbsp; &nbsp; private IDoSomething selectedVM;&nbsp; &nbsp; public IDoSomething SelectedVM&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; get { return selectedVM; }&nbsp; &nbsp; &nbsp; &nbsp; set&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedVM = value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedVM.doit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RaisePropertyChanged();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public ObservableCollection<IDoSomething> vms { get; set; } = new ObservableCollection<IDoSomething>&nbsp; &nbsp; {&nbsp; &nbsp;new uc1vm(),&nbsp; &nbsp; &nbsp; &nbsp; new uc2vm()&nbsp; &nbsp; };&nbsp; &nbsp; public MainWIndowViewModel()&nbsp; &nbsp; {&nbsp; &nbsp; }选择选项卡后,所选项目的设置器将被传递新值。强制转换并调用方法。我的界面非常简单,因为这只是说明性的:public interface IDoSomething{&nbsp; &nbsp; void doit();}一个示例视图模型,它只是说明性的,并没有做很多事情:public class uc1vm : IDoSomething{&nbsp; &nbsp; public string TabHeading { get; set; } = "Uc1";&nbsp; &nbsp; public void doit()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;// Your code goes here&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP