第一步是创建一个类:用于存放数据
前台界面是:
public class TreeModel : INotifyPropertyChanged
{
public string ID { get; set; }
public string Name { get; set; }
public int TypeID { get; set; }
public string ParentID { get; set; }
private bool? _shouldInstall;
public bool HasSubcomponents
{
get
{ return Childs.Count > 0; }
}
//是否允许Feature进行安置
public bool? ShouldInstall
{
get { return _shouldInstall; }
set
{
if (value != _shouldInstall)
{
_shouldInstall = value;
OnPropertyChanged("ShouldInstall");
}
}
}
public Collection<TreeModel> Childs { get; set; }
public TreeModel()
{
Childs = new Collection<TreeModel>();
ShouldInstall = false;
}
public event PropertyChangedEventHandler PropertyChanged;
//实现接口INotifyPropertyChanged定义函数
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
第二部
wcf 返回的数据中进行赋值
private void client_GetAgencyLineVehicleCompleted(object sender, GetAgencyLineVehicleCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else
{
if (e.Result != null)
{
AgencyLineVehicle agencyLineVehicle = e.Result;
ObservableCollection<Agency> AgencyCollection = agencyLineVehicle.Agency;
ObservableCollection<DepthDataService.Line> LineCollection = agencyLineVehicle.Line;
ObservableCollection<DepthDataService.Vehicle> VehicleCollection = agencyLineVehicle.Vehicle;
Collection<TreeModel> treeItems = new ObservableCollection<TreeModel>();
var agency = AgencyCollection.Where(a => Convert.ToInt32(a.ParentId) == 0);
foreach (var item in agency)
{
TreeModel treemodel = new TreeModel();
treemodel.ID = item.Id;
treemodel.Name = item.Name;
treemodel.ParentID = item.ParentId;
treeItems.Add(treemodel);
RecursionLine(LineCollection, VehicleCollection, treemodel);
RecursionAgecny(AgencyCollection, LineCollection, VehicleCollection, item, treemodel);
}
partTree.ItemsSource = treeItems; //进行绑定到treeview 数据源上
}
}
}
//递归机构的数据
private void RecursionAgecny(ObservableCollection<Agency> AgencyCollection, ObservableCollection<DepthDataService.Line> LineCollection, ObservableCollection<DepthDataService.Vehicle> VehicleCollection, Agency item, TreeModel treeItems)
{
var s = AgencyCollection.Where(a => Convert.ToInt32(a.ParentId) == Convert.ToInt32(item.Id));
if (s != null)
{
foreach (var child in s)
{
TreeModel treemodel = new TreeModel();
treemodel.ID = child.Id;
treemodel.Name = child.Name;
treemodel.ParentID = child.ParentId;
treemodel.TypeID = 0;
treeItems.Childs.Add(treemodel);
RecursionLine(LineCollection, VehicleCollection, treemodel);
RecursionAgecny(AgencyCollection, LineCollection, VehicleCollection, child, treemodel);
}
}
else
{
return;
}
}
//线路递归
private void RecursionLine(ObservableCollection<DepthDataService.Line> LineCollection, ObservableCollection<DepthDataService.Vehicle> VehicleCollection, TreeModel treeModel)
{
var s = LineCollection.Where(a => Convert.ToInt32(a.AgencyId) == Convert.ToInt32(treeModel.ID));
if (s != null)
{
foreach (var child in s)
{
TreeModel treemodel = new TreeModel();
treemodel.ID = child.Id;
treemodel.Name = child.Name;
treemodel.ParentID = child.AgencyId;
treemodel.TypeID = 1;
treeModel.Childs.Add(treemodel);
RecursionVehicle(VehicleCollection, treemodel);
RecursionLine(LineCollection, VehicleCollection, treemodel);
}
}
else
{
return;
}
}
前台界面是:
样式的抒写
<sdk:HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Childs}">
<!--<CheckBox Content="{Binding Name}"/>-->
<!--<TextBlock Text="{Binding Name}"></TextBlock>-->
<StackPanel Orientation="Horizontal">
<CheckBox
IsTabStop="False"
IsThreeState="{Binding HasSubcomponents}"
IsChecked="{Binding ShouldInstall, Mode=TwoWay}"
Click="ItemCheckbox_Click"
Content="{Binding Name}"
/>
<!--<ContentPresenter Content="{Binding Name}" />-->
</StackPanel>
</sdk:HierarchicalDataTemplate>
<sdk:TreeView Margin="0,2.5,0,2.5" BorderThickness="1" Height="472" x:Name="partTree" ItemTemplate="{StaticResource NodeTemplate}"/>