1

我无法使用此控件绑定内容。

我想创建一个自定义单元格模板,而不是默认的。使用默认单元格模板时,我的绑定有效。这是我的项目:

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>

代码背后:

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; }
}

为什么我的 ( <!-- Custom Column -->) 不显示员工姓名?我该怎么做才能将它绑定到该属性?如果我删除 CellTemplate 它可以工作,但我想要我的自定义样式......

4

1 回答 1

3

好的,我将在 Devex 团队之前回答 :)

改变这个:

<TextBlock Text="{Binding Path=Name}" Foreground="Blue" />

对此:

<TextBlock Text="{Binding Path=Data.Name}" Foreground="Blue" />

或者简单地说:

<TextBlock Text="{Binding Data.Name}" Foreground="Blue" />
于 2013-02-06T16:50:56.530 回答