Silverlight 4 TabControl MVVM选项卡标题绑定
在 silverlight 应用程序中,我在代码中动态创建选项卡项(MainView 的代码隐藏):
TabItem tab = new TabItem();
CustomerView view = new CustomerView();
view.DataContext = customerViewModel; //or tab.DataContext = customerViewModel;??
tab.Content = view;
DataTemplate template = this.Resources["CustomTabItemHeader"] as DataTemplate;
tab.HeaderTemplate = template;
tabControl.Items.Add(tab);
CustomTabItemHeader (在 MainView.xaml 中)如下所示:
<UserControl.Resources>
<DataTemplate x:Key="CustomTabItemHeader">
<TextBlock Text="{Binding Path=DisplayName}"/>
</DataTemplate>
</UserControl.Resources>
CustomerViewModel 具有 DisplayName 属性,并且它实现 INotifyPropertyChanged 接口。但 DisplayName 不会显示在选项卡标题中。有人可以解释一下为什么吗?
In silverlight app I create tab items dynamically in code (MainView's code-behind):
TabItem tab = new TabItem();
CustomerView view = new CustomerView();
view.DataContext = customerViewModel; //or tab.DataContext = customerViewModel;??
tab.Content = view;
DataTemplate template = this.Resources["CustomTabItemHeader"] as DataTemplate;
tab.HeaderTemplate = template;
tabControl.Items.Add(tab);
CustomTabItemHeader (in MainView.xaml) looks like:
<UserControl.Resources>
<DataTemplate x:Key="CustomTabItemHeader">
<TextBlock Text="{Binding Path=DisplayName}"/>
</DataTemplate>
</UserControl.Resources>
CustomerViewModel has DisplayName property and it implements INotifyPropertyChanged interface. But DisplayName isn't displayed in the tab header. Can someone explain me why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我愚蠢地假设 TabItem 标头模板中的文本块会查看选项卡的数据上下文以获得其文本值,但事实并非如此。所以我必须执行以下操作:
I foolishly assumed that textblock in the TabItem header template would look into tab's datacontext in order to get its text value, which was not the case. So I had to do the following: