轉(zhuǎn)帖|其它|編輯:郝浩|2011-04-15 11:18:41.000|閱讀 980 次
概述:背景 在很多時候 我們都希望得到一個無限制的樹形結(jié)構(gòu)來展示一層一層的數(shù)據(jù)。 尤其在SilverLight中更是常見。我見過網(wǎng)上很多用cs后天代碼來控制樹形控件的顯示層。 問題但是在MVVM模式中某些后臺代碼的控制就顯得不是很好用。本文主要介紹SilverLight的MVVM模式中模板實(shí)現(xiàn)無限級樹形結(jié)構(gòu),希望對大家有幫助。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
背景
在很多時候 我們都希望得到一個無限制的樹形結(jié)構(gòu)來展示一層一層的數(shù)據(jù)。
尤其在SilverLight中更是常見。
我見過網(wǎng)上很多用cs后天代碼來控制樹形控件的顯示層。
問題
但是在MVVM模式中某些后臺代碼的控制就顯得不是很好用。
解決方案
我們可以從樹形控件TreeView的ItemTemplate模板來實(shí)現(xiàn)這個小功能。
TreeView實(shí)際綁定一個最頂層的列表集合
Item綁定根據(jù)定層id獲取的下層列表集合
看效果:
數(shù)據(jù)庫設(shè)計:
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>
/// 供應(yīng)商分類列表
/// </summary>
public IList<SuppTypeModel> SuppTypeModelList
{
get { return _SuppTypeModelList; }
set
{
if (_SuppTypeModelList != value)
{
_SuppTypeModelList = value;
Notify(() => SuppTypeModelList);
}
}
}
/// <summary>
/// 供應(yīng)商子分類列表
/// </summary>
public IList<SuppTypeModel> ChildSuppTypeModelList
{
get { return _ChildSuppTypeModelList; }
set
{
if (_ChildSuppTypeModelList != value)
{
_ChildSuppTypeModelList = value;
Notify(() => ChildSuppTypeModelList);
}
}
}
/// <summary>
/// 獲取供應(yīng)商頂層列表數(shù)據(jù)
/// </summary>
private void GetSuppTypeList()
{
RequestService.Sent(DataMaintenanceUriNames.GetSuppTypeList, null,
delegate(object sent, ResponseArgs args)
{
var list = args.JsonResponse.GetPostData<IList<SuppTypeModel>>();
SuppTypeModelList = list;
});
}
//選擇了頂層Item顯示下層
//此處因?yàn)榭紤]到數(shù)據(jù)庫以后數(shù)據(jù)量大的話。全部獲取效率較低。。
//所以在點(diǎn)擊的時候只獲取一個即可
private void DoTreeViewSelection(object param)
{
if (param == null) return;
SuppTypeModel = (SuppTypeModel) param;
ChildSuppTypeModelList = new List<SuppTypeModel>();
ChildSuppTypeModelList.Add(SuppTypeModel);
GetSuppTypeListByID(SuppTypeModel);
}
//根據(jù)當(dāng)前選擇的Item來獲取他的下層集合
//因?yàn)槲以谶@里是用Hibernate配置的所以沒寫獲取的方法
//如果是常規(guī)獲取,直接可以變成數(shù)據(jù)庫獲取。
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" f
etch="subselect" where="SuppType_IsDel=0">
<key column="SuppType_ParentId" />
<one-to-many class="SuppTypeModel" />
</set>
</class>
</hibernate-mapping>
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客園