【发布时间】:2017-08-11 12:01:53
【问题描述】:
我正在尝试将级别 3 的列表绑定到 wpf mvvm 中的树视图。我通过调用模型类中的函数得到这个列表。我有如下的类结构。
namespace Projectname.Model
{
class Gate
{
public string Name { get; set; }
public string Location { get; set; }
public string IP { get; set; }
public int Port { get; set; }
public string GateMode { get; set; }
public string Type { get; set; }
public string Status { get; set; }
};
class Floor
{
public string Name { get; set; }
public List<Gate> Gates { get; set; }
};
class Building
{
public string Name { get; set; }
public List<Floor> Floors { get; set; }
};
}
以下是我尝试过的。这里我不知道如何给出数据类型。
<TreeView ItemsSource="{Binding Buildings}" Margin="10" Height="200">
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Buildings}" DataType="{x:Type local:Building}">
<TreeViewItem Header="{Binding Name}"/>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
属性
Window x:Class="projectname.MainWindow"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:local="clr-namespace:projectname.Model"
感谢任何帮助。
【问题讨论】:
标签: c# wpf mvvm binding treeview