Silverlight中的TabControl如何绑定数据

Silverlight中的TabControl如何绑定数据,第1张

概述在 WPF 中,TabControl 可以直接将 ItemsSource 绑定数据源 将 TabControl 绑定到数据的示例 http://msdn.microsoft.com/zh-cn/library/aa972130(VS.90).aspx   < Window.Resources > <

在 WPF 中,TabControl 可以直接将 ItemsSource 绑定数据源

将 TabControl 绑定到数据的示例

http://msdn.microsoft.com/zh-cn/library/aa972130(VS.90).aspx

 

          <      Window.Resources      >      
ObjectDataProvIDer x:Key ="TabListResource" ObjectType =" {x:Type src:TabList} " /> DataTemplate ="headerTemplate" TextBlock Text {Binding Path=header} </ DataTemplate ="ContentTemplate" {Binding Path=Content} DockPanel TabControl ItemsSource {Binding Source={StaticResource TabListResource}}
ItemTemplate
{StaticResource headerTemplate}
ContentTemplate
{StaticResource ContentTemplate} " >

 

匪疑所思的是,TabControl.ItemTeplate 居然是用于 header, ContentTemplate 才是用于 TabItem。

但是在 Silverlight 中貌似无此功能, TabControl.ContentTemplate  属性也不存在。

作为从 ItemsControl 中派生的类,Silverlight 中的 TabControl.ItemsSource 貌似要求为IEnumerable<TabItem>,

这让人很难接受, 不知是不是真的这样?

作为无奈的解决办法,只能对通过代码动态添加各个 TabItem,并对它们分别进行 Binding。

下面是代码:

namespace TabControl_DataBinding
{
using System;
System.ComponentModel;
System.Collections.ObjectModel;
System.ComponentModel.DataAnnotations;

public class Model
{
ObservableCollection < Article > Articles { get ; private set ; }
Model()
{
this .Articles = new ();
}
}
Article
{
string LanguageCode { Languagename { ; }
[display(name
= " 主题 " )]
Subject { ; }
[display(name
详细描述 Description { ; }
}

Settings
{
static System.Collections.Generic.Dictionary ,
Languages {
Settings()
{
Languages
();
Languages.Add(
en English );
Languages.Add(
zh-Hans 简体 zh-Hant 繁体 );
}
}
}

UserControl.Resources ="languageDataTemplate" GrID GrID.ColumnDeFinitions ColumnDeFinition WIDth ="auto" GrID.RowDeFinitions RowDeFinition Height ="*" sdk:Label GrID.Column ="0" GrID.Row
Target
{Binding Elementname=txtSubject} TextBox ="1" name ="txtSubject"
Text
{Binding Subject,Mode=TwoWay,
ValIDatesOnExceptions=true,NotifyOnValIDationError=true}
{Binding Elementname=txtDetail} ="txtDetail" WIDth ="280" {Binding Description,0)">
Height
="80" AcceptsReturn ="True" textwrapPing ="Wrap"
VerticalScrollbarVisibility
GrID GrID x:name ="LayoutRoot" Background ="White" sdk:TabControl name ="tabControl1" margin ="16" sdk:TabControl partial MainPage : UserControl
{
Model data;
MainPage()
{
InitializeComponent();
.Loaded += (sender,e) =>
{
.GenerateData();
.BindControls();
};
}
@H_750_403@///
<summary>
创建初始数据
@H_750_403@</summary>
@H_750_403@
voID GenerateData()
{
.data Model();
foreach (var item in Settings.Languages)
{
.data.Articles.Add( Article()
{
LanguageCode
item.Key,
Languagename
item.Value,
Subject
+ item.Value
});
}
}

BindControls()
{
Settings.Languages)
{
Article article
.data.Articles
.Single(art
art.LanguageCode == item.Key);
TabItem ti
TabItem()
{
header
article.Languagename,
Tag
article.LanguageCode,
ContentTemplate
(DataTemplate) .Resources[ languageDataTemplate ]
};
ti.SetBinding(TabItem.contentproperty,0)"> System.windows.Data.Binding());
.tabControl1.Items.Add(ti);
ti.DataContext
article;
}
}


}

总结

以上是内存溢出为你收集整理的Silverlight中的TabControl如何绑定数据全部内容,希望文章能够帮你解决Silverlight中的TabControl如何绑定数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1020349.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存