我本人接触siverlight时间不长,以前搞的东西太多太杂了,最近项目用到siverlight,而且siverlight体验很少一般啊。所以平时时间多,就用siverlight完善自己的文档收集系统了。
以前简单的用siverlight页面做了个增删改查的页面,好像结构有些问题。最近重构一下代码,将版块单独设置成一个数据表来存放,以前都是xml存放,感觉太不方便了,现在将分类和版块设置为子主关系了。
首先在服务器端生成entity framework和domainservice,metadata文件,本来可以不用生成元数据文件的,但是因为级联问题必须要生成了。
treeview第一级菜单是版块菜单,第二级是分类菜单,写在grid的资源文件,然后用树调用。
<Grid.Resources>
<sdk:HierarchicalDataTemplate x:Key="ChildTemplate">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</sdk:HierarchicalDataTemplate>
<sdk:HierarchicalDataTemplate x:Key="RootTemplate" ItemsSource="{Binding Fenlei}"
ItemTemplate="{StaticResource ChildTemplate}">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</sdk:HierarchicalDataTemplate>
</Grid.Resources>
<sdk:TreeView Height="403" HorizontalAlignment="Left" Name="treeView1"
VerticalAlignment="Top" Width="192" ItemTemplate="{StaticResource RootTemplate}" />
这个树的构成不多说,有大牛的文章介绍,http://www.cnblogs.com/jv9/archive/2009/12/12/1621952.html
后台绑定就没法求人了
用在构造函数中用回调方法得到数据库中的版块实体,但是由于二级菜单是分类实体,虽然分类实体是版块实体的儿子吧,表述比较乱啊,所以在调用版块实体时应该关联分类实体,就需要用到include特性了。
所以在metedata中定义,
[Include]
public BankuaiInfo BankuaiInfo { get; set; }
在版块实体的查询中定义,
public IQueryable<BankuaiInfo> GetBankuaiInfos()
{
return this.ObjectContext.BankuaiInfos.Include("Fenlei");
}
最后写页面回调函数,并绑定treeview
MyAppContext db = new MyAppContext();
public FenleiEdit()
{
InitializeComponent();
InitData();
}
void InitData()
{
//这个和底下那句,是给页面加加载控件的,可以注释掉
((System.Windows.Controls.BusyIndicator)(App.Current.RootVisual)).IsBusy = true;
db.Load(db.GetBankuaiInfosQuery(), LoadBehavior.RefreshCurrent, loadBankuaiInfo, null);
}
void loadBankuaiInfo(LoadOperation<BankuaiInfo> lo)
{
ObservableCollection<BankuaiInfo> forumList = new ObservableCollection<BankuaiInfo>(lo.Entities);
this.treeView1.ItemsSource = forumList;
((System.Windows.Controls.BusyIndicator)(App.Current.RootVisual)).IsBusy = false;
}
机子好卡,写到这里,以后再写一个对节点增删改查的。