编码器!
更新
解决方案很简单:确保您阅读的属性具有明确的getter。
Foo{ get; private set; }
我有一个问题: 首先要知道的是:我必须为WPF和Silverlight使用相同的.xaml文件。
我想要一个包含“Employee”类型节点的TreeView,比如说。 员工可以让员工为他工作,或者可能没有员工。
我有一个列表(Collection),我可以将其用作ItemsSource。 Employee类包含一个Employees集合:如果没有人为他工作则为空(非空),而当他是某些人的老板时则为非空。
我找到了很好的例子:http://msdn.microsoft.com/en-us/library/cc165616(v=vs.110).aspx http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx
我有一个“MyTreeView
”类,我也可以用于WPF和Silverlight,同样适用于“MyHierarchicalDataTemplate
”,也适用于“MyTreeViewItem
”,所以没问题这里。
我的资源部分包含:
<local:MyHierarchicalDataTemplate x:Key="treeTemplate" ItemsSource="{Binding Employees}">
<StackPanel Orientation="Horizontal">
<MyControls:MyTreeViewItem Header="{Binding Name}">
<StackPanel Orientation="Horizontal">
<TextBlock Width="40" Text="{Binding Age}" />
<!-- and so on... -->
</StackPanel>
<ListBox ItemsSource="{Binding Employees}" />
<!-- I tried to make it recursive this way. Result: exception or malfunction. -->
<!--<MyControls:MyTreeView ItemsSource="{Binding Path=Relations}">
</MyControls:MyTreeView>-->
</MyControls:MyTreeViewItem>
</StackPanel>
</local:MyHierarchicalDataTemplate>
员工数据结构:
Big "Boss" John
-- Fred
-- George
-- Harry
---- Snape
---- Dumbledore
------ Alastor
------ Hermione
-- Mark
-- Steve
---- Stewie
------ Dad
-------- Meg
-------- Death
------ Mother
------ World
问题:
如何填写TreeView?每个节点和叶子都是“Employee
”类型
请记住:困难的部分是使其在WPF和Silverlight中运行。
备注
我尝试DataType="{x:Type local:Employee}"
(或类似),但我的x命名空间似乎不包含x:Type。
似乎我不能使用XmlDataProvider
。
我想尽可能多地保留MVC / MVVC模型。
相关名称空间:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
课程Employee
的代码隐藏
public class Employee
{
public Employee()
{
this.StationFromId = "";
this.StationFromName = "";
this.StationToId = "";
this.StationToName = "";
this.BackGroundColor = Colors.White;
this.ForeGroundColor = Colors.Black;
}
public Employee(string fromStationId, string name, int age, List<Employee> children)
{
this.Name = name;
this.Age = age;
if (children != null)
{
this.Employees = new Collection<Employee>(children);
}
}
public string Name{ get; private set; }
public int Age{ get; private set; }
public Color BackGroundColor = Colors.White;
public Color ForeGroundColor = Colors.Black;
public Collection<ServiceIntentionRelation> Employees = new Collection<Employee>();
}
我为所有东西创建了一个DataContext,这是用的。
public class EmployeeDataContext
{
public EmployeeDataContext()
{
this.Fill();
}
public Collection<Employee> Employees = null; // I could use new Collection<Employee>(), does not matter (theoretically)
private void Fill()
{
var e1 = new Employee("George", 34, null);
var e2 = new Employee("Steve", 19, null);
// e3, e4 and e5 is made the very same way
var s6 = new Employee("Mike", 56, new List<Employee>(){e3,e4,e5});
this.Relations = new Collection<ServiceIntentionRelation>() { e1, e2, e6 };
// e6 has e3, e4, e5 as children.
}
}
感谢您的回答!
答案 0 :(得分:1)
尝试以这种方式定义分层数据模板。
<local:MyHierarchicalDataTemplate x:Key="treeTemplate" ItemsSource="{Binding Employees}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="0,0,4,0"/>
<TextBlock Width="40" Text="{Binding Age}" />
</StackPanel>
</local:MyHierarchicalDataTemplate>
这会将treeviewitem的标题设置为StackPanel中的信息,并将treeviewitem的ItemsSource设置为Employees。
treeviewitem的itemscontainer是另一个treeviewitem,因此当遇到员工时,使用相同的datatemplate创建树视图项。这就是层次结构的创建方式。