以编程方式将 TreeViewItem 绑定到自定义列表 object

Programmatically binding TreeViewItem to List of custom object

如何以编程方式设置将 newTableList.Items 个元素的 "Header" 属性 绑定到 TableModel.TABLE_NAME?

foreach (SchemaModel schema in connection.schemas)
{ 
 TreeViewItem newSchema = new TreeViewItem() 
 { 
     Header = schema.SCHEMA_NAME.ToString() 
 };
 Binding newTableBinding = new Binding();     
 newTableBinding.Source = schema.tables;

 TreeViewItem newTableList = new TreeViewItem()
 {
      Header = "Tables",
 };

 BindingOperations.SetBinding( newTableList, TreeViewItem.ItemsSourceProperty, newTableBinding);

newSchema.Items.Add(newTableList);
newTVI.Items.Add(newSchema);

}

我以前的非常慢的代码看起来像这样:

foreach (TableModel table in schema.tables)
{
   newTableList.Items.Add(new TreeViewItem()
   {
       Header = table.TABLE_NAME.ToString()
   });
}

老话题(更好看)

我尝试构建自定义 TreeView 并以最快的速度更改我的 "VERY SLOW METHOD" 并绑定到自定义列表 objects。

我有包含

的 SchemaModel
List<TableModel> tables

并且每个 TableModel 都有

string TABLE_NAME.

我以前的非常慢的方法是:

/*  VERY SLOW METHOD !!! */
//foreach (TableModel table in schema.tables)
//{
//    newTableList.Items.Add(new TreeViewItem()
//    {
//        Header = table.TABLE_NAME.ToString()
//    });
//}

每次创建 TreeViewItem 都会减慢我的 UI,我无法通过多任务处理修复它。

我决定以编程方式绑定到这样的 TableModel 列表:

Binding newTableBinding = new Binding();
newTableBinding.Source = schema.tables;

TreeViewItem newTableList = new TreeViewItem()
{
    Header = "Tables",
    // ItemsSource = schema.tables // also works 
};
BindingOperations.SetBinding( newTableList, TreeViewItem.ItemsSourceProperty, newTableBinding);

如何将基于 schema.tables 列表的项目的 Header 属性 绑定到 "TABLE_NAME"?

我的完整代码

代码:

foreach (ConnectionModel connection in aliases)
{
    TreeViewItem newTVI = new TreeViewItem() { Header = connection.alias.ToString() };

    foreach (SchemaModel schema in connection.schemas)
    {
        TreeViewItem newSchema = new TreeViewItem() { Header = schema.SCHEMA_NAME.ToString() };

        Binding newTableBinding = new Binding();
        newTableBinding.Source = schema.tables;
        // newTableBinding.Path = new PropertyPath("TABLE_NAME");

        TreeViewItem newTableList = new TreeViewItem()
        {
            Header = "Tables",
            // ItemsSource = schema.tables
        };
        BindingOperations.SetBinding( newTableList, TreeViewItem.ItemsSourceProperty, newTableBinding);

       TreeViewItem newIndexList = new TreeViewItem() { Header = "Indexes" };

      /*  VERY SLOW METHOD !!! */
       //foreach (TableModel table in schema.tables)
       //{
       //    newTableList.Items.Add(new TreeViewItem()
       //    {
       //        Header = table.TABLE_NAME.ToString()
       //    });
       //}

       newSchema.Items.Add(newTableList);
       newSchema.Items.Add(newIndexList);
       newTVI.Items.Add(newSchema);
   }
   tmpAliasTree.Items.Add(newTVI);
}

tmpAliasTree 是我的 TreeView。

我的连接模型

[Serializable()]
public class ConnectionModel
{

    private int    _id;

    private string _dsn;
    private string _alias   ;

    private string _host    ;
    private string _port    ;

    private string _database;

    private string _username;
    private string _password;

    public List<SchemaModel> schemas = new List<SchemaModel>();

  }

我的架构模型 :

[Serializable()]
public class SchemaModel
{
    [System.Xml.Serialization.XmlElement("SCHEMA_NAME")]
    public string SCHEMA_NAME { get; set; } = "";

    [XmlArray("tables"), XmlArrayItem("TableModel", typeof(TableModel), ElementName = "TableModel")]
    public List<TableModel> tables = new List<TableModel>();

}

我的表格模型

[Serializable()]
public class TableModel
{
    [System.Xml.Serialization.XmlElement("TABLE_CAT")]
    public string TABLE_CAT     { get; set; }  = "";
    [System.Xml.Serialization.XmlElement("TABLE_SCHEM")]
    public string TABLE_SCHEM   { get; set; }  = "";
    [System.Xml.Serialization.XmlElement("TABLE_NAME")]
    public string TABLE_NAME    { get; set; }  = "";
    [System.Xml.Serialization.XmlElement("TABLE_TYPE")]
    public string TABLE_TYPE    { get; set; }  = "";
    [System.Xml.Serialization.XmlElement("REMARKS")]
    public string REMARKS       { get; set; } = "";
}

感谢您的任何建议。

尽管我同意您应该考虑将视图定义移动到 XAML,但您可以通过利用 ItemsControl.ItemContainerStyle 属性(两者都 TreeView 来实现您的要求TreeViewItem 派生自 ItemsControl)。基本上,您需要定义一个针对 TreeViewItem 的样式,并为 TreeViewItem.HeaderProperty 添加一个 setter,其值持有适当的绑定,然后将该样式分配给您的树视图或特定项目(取决于您的需要)。这是一个例子:

TreeViewItem newTVI = new TreeViewItem() { Header = connection.alias.ToString() };
var tableModelItemStyle = new Style(typeof(TreeViewItem));
tableModelItemStyle.Setters.Add(new Setter
{
    Property = TreeViewItem.HeaderProperty,
    //since items will present instances of TableModel, the DataContext will hold
    //the model, so we can define the binding using only the property name
    Value = new Binding("TABLE_NAME"),
});
foreach(...)
{
    ...
    TreeViewItem newTableList = new TreeViewItem
    {
        ...
        ItemContainerStyle = tableModelItemStyle,
    };
    ...
}

如果你想为树视图中的所有项目设置样式(我不推荐),你可以这样做:

newTVI.ItemContainerStyle = tableModelItemStyle;