【发布时间】:2010-11-25 10:39:38
【问题描述】:
在 Prism V4 / MEF / MVVM 应用程序中,我得到了一个包含 TabControl 的视图。
在第一个 TabItem 中,我显示了一个项目列表,第二个 TabItem 被禁用,除非选择了一个有效的项目。现在,当用户单击第二个 TabItem 时,我想将一些附加数据加载并准备到第二个 TabItem 中。
如何获得 MVVM 中 TabItem 更改的通知?
【问题讨论】:
在 Prism V4 / MEF / MVVM 应用程序中,我得到了一个包含 TabControl 的视图。
在第一个 TabItem 中,我显示了一个项目列表,第二个 TabItem 被禁用,除非选择了一个有效的项目。现在,当用户单击第二个 TabItem 时,我想将一些附加数据加载并准备到第二个 TabItem 中。
如何获得 MVVM 中 TabItem 更改的通知?
【问题讨论】:
我认为您的意思是延迟加载。启动此示例并将调试断点放入 ContentViewModel 构造函数中。
public MainWindow()
{
InitializeComponent();
var items = new List<TabItemViewModel>
{ new TabItemViewModel{Title="Tab 1", Content = new Lazy<ContentViewModel>(() => new ContentViewModel(1))},
new TabItemViewModel{Title="Tab 2", Content = new Lazy<ContentViewModel>(() => new ContentViewModel(2))}
};
tab.ItemsSource = items;
}
public class TabItemViewModel
{
public string Title { get; set; }
public Lazy<ContentViewModel> Content { get; set; }
}
public class ContentViewModel
{
public ContentViewModel(int i)
{
this.SomeText = "Loaded tab "+i;
}
public string SomeText { get; set; }
}
Xaml 模板:
<TabControl x:Name="tab">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding Content.Value.SomeText}"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
【讨论】: