Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DevExpress TreeListControl创建自定义单元格模板_C#_Wpf_Xaml_Devexpress - Fatal编程技术网

C# DevExpress TreeListControl创建自定义单元格模板

C# DevExpress TreeListControl创建自定义单元格模板,c#,wpf,xaml,devexpress,C#,Wpf,Xaml,Devexpress,我在用电脑装订东西时遇到了麻烦 我想创建一个自定义单元格模板,而不是默认的。使用默认单元格模板时,我的绑定工作正常。以下是我的项目: XAML: <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/200

我在用电脑装订东西时遇到了麻烦

我想创建一个自定义单元格模板,而不是默认的。使用默认单元格模板时,我的绑定工作正常。以下是我的项目:

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

代码隐藏:

<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; }
}
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Documents;
命名空间WpfApplication2
{
公共部分类主窗口:窗口
{        
公共主窗口()
{
初始化组件();
treeList.ItemsSource=GetStuff();
ExpandAllNodes();
}
公共静态列表GetStuff()
{
List stuff=新列表();
添加(新员工(){ID=1,ParentID=0,Name=“Gregory S.Price”});
Add(新员工(){ID=2,ParentID=0,Name=“Irma R.Marshall”});
Add(新员工(){ID=3,ParentID=0,Name=“johnc.Powell”});
Add(新员工(){ID=6,ParentID=2,Position=“Brian C.Cowling”});
Add(新员工(){ID=7,ParentID=2,Position=“Thomas C.Dawson”});
Add(新员工(){ID=8,ParentID=2,Position=“angelm.Wilson”});
Add(新员工(){ID=9,ParentID=2,Position=“Bryan R.Henderson”});
Add(新员工(){ID=10,ParentID=3,Position=“Harold S.Brandes”});
Add(新员工(){ID=11,ParentID=3,Position=“Michael S.Blevins”});
Add(新员工(){ID=12,ParentID=3,Position=“Jan K.Sisk”});
添加(新员工(){ID=13,ParentID=3,Position=“Sidney L.Holder”});
Add(新员工(){ID=14,ParentID=1,Position=“Brian C.Cowling”});
Add(新员工(){ID=15,ParentID=1,Position=“Thomas C.Dawson”});
Add(新员工(){ID=16,ParentID=1,Position=“angelm.Wilson”});
Add(新员工(){ID=17,ParentID=1,Position=“Bryan R.Henderson”});
归还物品;
}        
}
}
公营雇员
{
公共int ID{get;set;}
public int ParentID{get;set;}
公共字符串名称{get;set;}
公共字符串位置{get;set;}
}

为什么我的(
)没有显示员工的姓名?我该怎么做才能把它绑定到那个财产上?如果我删除了CellTemplate,它可以工作,但我想拥有我的自定义样式…

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

更改此项:

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

为此:

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

或者简单地说:

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


非常确定您想要的是Path=Row。Name@Aron不。在发布之前,我已经测试了我的答案。100%肯定。效果很好,你的回答速度比Devex团队快得多!以下是devExpress的问题:@TonoNam我不能责怪他们,他们每天都要处理100个问题。另外,他们现在可能已经下班了。在我看来,可以使用TreeListColumn的FieldName,而不是对每一列使用重写的CellTemplates