Hi,
I've tried to create a treeview, however I've had problem in showing the data in the treeview. Let me show how the treeview would be.
Table 01
---- Fields
-------- Field 01
-------- RelatedField 01
-----RelatedTables
-------- Table R 01
------------ Fields
------------------ Field 01
- RelatedTable
Table 02
---- Fields
-------- Field 01
-------- RelatedField 01
-----RelatedTables
-------- Table R 01
------------ Fields
------------------ Field 01
- RelatedTable
So, Here are the classes:
public interface ITableField { string Name { get; set; } string Alias { get; set; } Table Parent { get; } } public class MainTableCollection: ObservableCollection<Table> { public void AddRange(IEnumerable<Table> tables) { tables.ToList().ForEach(Add); } } public sealed class RelatedTableCollection : ObservableCollection<Table> { public void AddRange(IEnumerable<Table> relatedTables) { relatedTables.ToList().ForEach(Add); } } public sealed class RelatedTableField : PropertyChangedBase, ITableField { private string _name; public string Name { get { return _name; } set { _name = value; NotifyOfPropertyChange(() => Name); } } private string _alias; public string Alias { get { return _alias; } set { _alias = value; NotifyOfPropertyChange(() => Alias); } } private Table _parent; public Table Parent { get { return _parent; } set { _parent = value; NotifyOfPropertyChange(() => Parent); } } private TableField _originalField; public TableField OriginalField { get { return _originalField; } set { _originalField = value; NotifyOfPropertyChange(() => OriginalField); } } } public sealed class Table : PropertyChangedBase { public Table() { Fields = new TableFieldCollection(); RelatedTables = new RelatedTableCollection(); } private string _name; public string Name { get { return _name; } set { _name = value; NotifyOfPropertyChange(() => Name); } } private string _alias; public string Alias { get { return _alias; } set { _alias = value; NotifyOfPropertyChange(() => Alias); } } public RelatedTableCollection RelatedTables { get; private set; } public TableFieldCollection Fields { get; private set; } } public sealed class TableField : PropertyChangedBase, ITableField { private string _name; public string Name { get { return _name; } set { _name = value; NotifyOfPropertyChange(() => Name); } } private string _alias; public string Alias { get { return _alias; } set { _alias = value; NotifyOfPropertyChange(() => Alias); } } private Table _parent; public Table Parent { get { return _parent; } set { _parent = value; NotifyOfPropertyChange(() => Parent); } } } public sealed class TableFieldCollection : ObservableCollection<ITableField> { public void AddRange(IEnumerable<ITableField> fields) { fields.ToList().ForEach(Add); } }
Now, here are the view and viewmodel:
public class TableTreeViewModel : Screen, IViewModelIdentity { #region Fields and Constants private readonly IEventAggregator _events; //private const string PREVIOUS = "BackendGeneratorViewModel"; #endregion #region Constructors [ImportingConstructor] public TableTreeViewModel(IEventAggregator events) { _events = events; this.ScreenIdentity = this.GetType().Name; MainTables = new MainTableCollection(); MainTables.AddRange(createMainTables()); } #endregion #region Properties public string ScreenIdentity { get; set; } public MainTableCollection MainTables { get; set; } #endregion #region Methods private IEnumerable<Table> createMainTables() { var MainTables = new List<Table>(); var adsp010 = new Table { Name = "ADSP010", Alias = "Pessoa" }; adsp010.Fields.Add(new TableField{Alias = "Código", Name = "nId", Parent = adsp010}); adsp010.Fields.Add(new TableField { Alias = "Nome", Name = "cNome", Parent = adsp010 }); var adsp044 = new Table { Name = "ADSP044", Alias = "Fornecedor" }; adsp044.Fields.Add(new TableField { Alias = "Código", Name = "nId", Parent = adsp044 }); adsp044.Fields.Add(new TableField { Alias = "Descricao", Name = "cDescricao", Parent = adsp044 }); adsp044.Fields.Add(new RelatedTableField { Alias = "NomeFornecedor", Name = "cNomeRelated", Parent = adsp044, OriginalField = adsp010.Fields[1] as TableField}); adsp044.RelatedTables.Add(adsp010); MainTables.Add(adsp044); return MainTables; } #endregion
<TreeView Grid.Column="0" ItemsSource="{Binding MainTables}"><TreeView.Resources><HierarchicalDataTemplate DataType="{x:Type table:Table}"><Border Style="{StaticResource DarkBlueBorderStyle}"><StackPanel Orientation="Horizontal"><Image Source="pack://application:,,,/Resources/table.png"/><TextBlock Text="{Binding Alias}" Foreground="#003D50" Margin="5,0,0,0"/></StackPanel></Border></HierarchicalDataTemplate><HierarchicalDataTemplate DataType="{x:Type table:RelatedTableCollection}"><Border Style="{StaticResource DarkGrayBorderStyle}"><StackPanel Orientation="Horizontal"><Image Source="pack://application:,,,/Resources/relatedTable.png"/><TextBlock Text="{Binding Alias}" Foreground="#2F2F2F" Margin="5,0,0,0"/></StackPanel></Border></HierarchicalDataTemplate><HierarchicalDataTemplate DataType="{x:Type table:TableFieldCollection}"><Border Style="{StaticResource DarkBlueBorderStyle}"><StackPanel Orientation="Horizontal"><Image Source="pack://application:,,,/Resources/tableField.png"/><TextBlock Text="{Binding Alias}" Foreground="#003D50" Margin="5,0,0,0"/></StackPanel></Border></HierarchicalDataTemplate><HierarchicalDataTemplate DataType="{x:Type table:TableField}"><TextBlock Text="{Binding Alias}" Foreground="#000000" Margin="3"/></HierarchicalDataTemplate><DataTemplate DataType="{x:Type table:RelatedTableField}"><TextBlock Text="{Binding Alias}" Foreground="#6A6A6A" FontWeight="Medium" Margin="3"/></DataTemplate></TreeView.Resources></TreeView>
It's not clear for me in creating HierarchicalDataTemplate. I thought it was correct, however shows just a table in the hierarchy.
Did anybody already have this problem?
Thank you very much!