SilverLight的MVVM模式中模板实现无限级树形结构(TreeView)

背景

 


在很多时候 我们都希望得到一个无限制的树形结构来展示一层一层的数据。。。
尤其在SilverLight中更是常见。。。
我见过网上很多用cs后天代码来控制树形控件的显示层。。。


问题 
但是在MVVM模式中某些后台代码的控制就显得不是很好用。。。


解决方案
我们可以从树形控件TreeView的ItemTemplate模板来实现这个小功能。。。
TreeView实际绑定一个最顶层的列表集合
Item绑定根据定层id获取的下层列表集合
看效果:

数据库设计:

SilverLight代码:

<sdk:TreeView HorizontalAlignment="Stretch" x:Name="tvSuppType"
VerticalAlignment="Stretch" Margin="5" ItemsSource="{Binding SuppTypeModelList}"
  BorderBrush="#687B8B">
<sdk:TreeView.ItemTemplate>
<sdk:HierarchicalDataTemplate ItemsSource="{Binding ChildSuppTypeModelList}">
<ContentPresenter Content="{Binding SuppType_Name}" />
</sdk:HierarchicalDataTemplate>
</sdk:TreeView.ItemTemplate>

</sdk:TreeView>

cs代码:

/// <summary>
/// 供应商分类列表
/// </summary>
public IList<SuppTypeModel> SuppTypeModelList
{
get { return _SuppTypeModelList; }
set
{
if (_SuppTypeModelList != value)
{
_SuppTypeModelList
= value;
Notify(()
=> SuppTypeModelList);
}
}
}

/// <summary>
/// 供应商子分类列表
/// </summary>
public IList<SuppTypeModel> ChildSuppTypeModelList
{
get { return _ChildSuppTypeModelList; }
set
{
if (_ChildSuppTypeModelList != value)
{
_ChildSuppTypeModelList
= value;
Notify(()
=> ChildSuppTypeModelList);
}
}
}

/// <summary>
/// 获取供应商顶层列表数据
/// </summary>
private void GetSuppTypeList()
{
RequestService.Sent(DataMaintenanceUriNames.GetSuppTypeList,
null,
delegate(object sent, ResponseArgs args)
{
var list
=
args.JsonResponse.GetPostData<IList<SuppTypeModel>>();
SuppTypeModelList
= list;

});
}
//选择了顶层Item显示下层
//此处因为考虑到数据库以后数据量大的话。全部获取效率较低。。
//所以在点击的时候只获取一个即可
private void DoTreeViewSelection(object param)
{
if (param == null) return;
SuppTypeModel
= (SuppTypeModel) param;
ChildSuppTypeModelList
= new List<SuppTypeModel>();
ChildSuppTypeModelList.Add(SuppTypeModel);
GetSuppTypeListByID(SuppTypeModel);
}

//根据当前选择的Item来获取他的下层集合
//因为我在这里是用Hibernate配置的所以没写获取的方法
//如果是常规获取,直接可以变成数据库获取。
private void GetSuppTypeListByID(SuppTypeModel SuppType)
{
foreach (SuppTypeModel suppTypeModel in SuppType.ChildSuppTypeModelList)
{
ChildSuppTypeModelList.Add(suppTypeModel);
GetSuppTypeListByID(suppTypeModel);
}
}

Model代码:

///<summary>
/// 父模型
///</summary>
[JsonIgnore]
public virtual SuppTypeModel FatherSuppTypeModel { get; set; }
private ICollection<SuppTypeModel> _ChildSuppTypeModel = new HashSet<SuppTypeModel>();
/// <summary>
/// 子模型集合
/// </summary>
public virtual ICollection<SuppTypeModel> ChildSuppTypeModelList
{
set { _ChildSuppTypeModel = value; }
get
{
if (_ChildSuppTypeModel != null)
{
foreach (SuppTypeModel areaModel in _ChildSuppTypeModel)
{
areaModel.FatherSuppTypeModel
= this;
}
}

return _ChildSuppTypeModel;
}
}

Hibernate获取顶层集合

/// <summary>
/// 获取往来单位.分类树形列表
/// </summary>
/// <returns>IList<SuppTypeModel/></returns>
[Transaction(ReadOnly = true)]
public IList<SuppTypeModel> GetSuppTypeList()
{
IList
<SuppTypeModel> list = DaoSupport.Session.CreateCriteria<SuppTypeModel>()
.Add(Restrictions.Eq(
"SuppType_IsDel", "0"))
.Add(Restrictions.Eq(
"SuppType_ParentId",
 new Guid("00000000-0000-0000-0000-000000000000")))
.List
<SuppTypeModel>();
return list;
}

Hibernate配置文件

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
default-lazy="false" namespace="MMISUI.Models.Area" assembly="MMISUI">
<class name="SuppTypeModel" table="T_Bas_SuppType">
<id name="SuppType_Id" column="SuppType_Id">
<generator class="guid"></generator>
</id>
<property name="SuppType_Code"></property>
<property name="SuppType_Name"></property>
<property name="SuppType_CodeLevel"></property>
<property name="SuppType_ParentId"></property>
<property name="SuppType_IsEnd"></property>
<property name="SuppType_IsDel"></property>
<property name="SuppType_Remark"></property>
<many-to-one name="FatherSuppTypeModel" class="SuppTypeModel"
column="SuppType_ParentId" not-found="ignore" update="false" insert="false"></many-to-one>
<set name="ChildSuppTypeModelList" fetch="subselect" where="SuppType_IsDel=0">
<key column="SuppType_ParentId" />
<one-to-many class="SuppTypeModel" />
</set>

</class>

</hibernate-mapping>


代码有点乱。。。见谅!!!
大功告成!!!


作者:记忆逝去的青春
出处:http://www.cnblogs.com/lukun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过http://www.cnblogs.com/lukun/  联系我,非常感谢。

posted on 2011-04-14 10:50  记忆逝去的青春  阅读(2146)  评论(1编辑  收藏  举报