wpf DevExpress TreeListControl 创建自定义单元格模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14733451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DevExpress TreeListControl create custom cell template
提问by Tono Nam
I am having trouble binding stuff with THIS CONTROL.
I want to create a custom cell template not the default one. When using the default cell template my binding works. Here is my project:
我想创建一个自定义单元格模板而不是默认模板。使用默认单元格模板时,我的绑定有效。这是我的项目:
XAML:
XAML:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:dxt="http://schemas.devexpress.com/winfx/2008/xaml/grid" >
<Window.Resources>
</Window.Resources>
<Grid>
<dxt:TreeListControl Name="treeList">
<dxt:TreeListControl.Columns>
<!-- Custom Column -->
<dxt:TreeListColumn FieldName="Name" Header="Name">
<dxt:TreeListColumn.CellTemplate>
<DataTemplate >
<TextBlock Text="{Binding Path=Name}" Foreground="Blue" />
</DataTemplate>
</dxt:TreeListColumn.CellTemplate>
</dxt:TreeListColumn>
<!-- default Column -->
<dxt:TreeListColumn FieldName="Position" Header="Position"/>
</dxt:TreeListControl.Columns>
<dxt:TreeListControl.View>
<dxt:TreeListView Name="treeListView1" AutoWidth="True"
KeyFieldName="ID" ParentFieldName="ParentID"/>
</dxt:TreeListControl.View>
</dxt:TreeListControl>
</Grid>
</Window>
Code Behind:
背后的代码:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow ( )
{
InitializeComponent( );
treeList.ItemsSource = GetStuff( );
treeListView1.ExpandAllNodes( );
}
public static List<Employee> GetStuff ( )
{
List<Employee> stuff = new List<Employee>( );
stuff.Add( new Employee( ) { ID = 1 , ParentID = 0 , Name = "Gregory S. Price" } );
stuff.Add( new Employee( ) { ID = 2 , ParentID = 0 , Name = "Irma R. Marshall" } );
stuff.Add( new Employee( ) { ID = 3 , ParentID = 0 , Name = "John C. Powell" } );
stuff.Add( new Employee( ) { ID = 6 , ParentID = 2 , Position = "Brian C. Cowling" } );
stuff.Add( new Employee( ) { ID = 7 , ParentID = 2 , Position = "Thomas C. Dawson" } );
stuff.Add( new Employee( ) { ID = 8 , ParentID = 2 , Position = "Angel M. Wilson" } );
stuff.Add( new Employee( ) { ID = 9 , ParentID = 2 , Position = "Bryan R. Henderson" } );
stuff.Add( new Employee( ) { ID = 10 , ParentID = 3 , Position = "Harold S. Brandes" } );
stuff.Add( new Employee( ) { ID = 11 , ParentID = 3 , Position = "Michael S. Blevins" } );
stuff.Add( new Employee( ) { ID = 12 , ParentID = 3 , Position = "Jan K. Sisk" } );
stuff.Add( new Employee( ) { ID = 13 , ParentID = 3 , Position = "Sidney L. Holder" } );
stuff.Add( new Employee( ) { ID = 14 , ParentID = 1 , Position = "Brian C. Cowling" } );
stuff.Add( new Employee( ) { ID = 15 , ParentID = 1 , Position = "Thomas C. Dawson" } );
stuff.Add( new Employee( ) { ID = 16 , ParentID = 1 , Position = "Angel M. Wilson" } );
stuff.Add( new Employee( ) { ID = 17 , ParentID = 1 , Position = "Bryan R. Henderson" } );
return stuff;
}
}
}
public class Employee
{
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
}
why is it that my (<!-- Custom Column -->) does not display the Name of the employe? what do I have to do to bind it to that Property? If I remove the CellTemplate it works but I want to have my custom style...
为什么我的 ( <!-- Custom Column -->) 没有显示员工姓名?我该怎么做才能将其绑定到该属性?如果我删除 CellTemplate 它可以工作但我想要我的自定义样式...
回答by Abdusalam Ben Haj
Ok I'm going to answer before Devex Team :)
好的,我将在 Devex 团队之前回答:)
Change this :
改变这一点:
<TextBlock Text="{Binding Path=Name}" Foreground="Blue" />
To this :
对此:
<TextBlock Text="{Binding Path=Data.Name}" Foreground="Blue" />
Or simply :
或者干脆:
<TextBlock Text="{Binding Data.Name}" Foreground="Blue" />

