繁体   English   中英

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

[英]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并通过绑定到自定义对象列表来最快地更改“非常慢的方法”。

我有包含的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。

我的ConnectionModel

[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>();

  }

我的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属性( TreeViewTreeViewItem都从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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM