Search code examples
c#wpfdata-bindingtreeviewitemtreelistview

Populating columns of a window


What I am trying to do is populate two columns in a window. This columns will have the name of the global variable and the path it to it. I am having problems displaying what I need in the windows.

        <TabItem Header="Global Variables" GotFocus="GlobalVariablesTab_GotFocus">
           <dc:TreeListView Name="tvwGlobalVariables"  dc:ColumnLayoutManager.Enabled="True" >
                <dc:TreeListView.Columns>

                    <!--First Column. -->
                   <dc:StdGridViewColumn Header="Variable" Width="200" >

                         <dc:StdGridViewColumn.CellTemplate>
                             <DataTemplate>
                                <TextBlock TextTrimming="CharacterEllipsis"/>
                            </DataTemplate>
                         </dc:StdGridViewColumn.CellTemplate>
                    </dc:StdGridViewColumn>

                    <!-- Second Column-->
                     <dc:StdGridViewColumn Header="Result" dc:ColumnRange.IsFillColumn="True"/>

               </dc:TreeListView.Columns> 
            </dc:TreeListView> 
        </TabItem>

This is the tab that has the area I need to populate. The first column for the name and the second will be for the path. I may be missing something as well.

    private void GlobalVariablesTab_GotFocus(object sender, RoutedEventArgs e)
    {
        lvGlobals.Items.Clear();
        sman = SchemaManager.SchemaManager.GetInstance();
        IEnumerator enumerator = sman.GetGlobalVariableEnumerator();
        while (enumerator.MoveNext()) 
        {
            DictionaryEntry entry = (DictionaryEntry) enumerator.Current;
            ListViewItem lvi = new ListViewItem();//new string[] {entry.Key.ToString(), entry.Value.ToString()});
            lvi.Tag = entry.Key.ToString() + entry.Value.ToString();
        }
    }

Currently I have lvi holding both parts but I will need one part to go to the first column and the other part to the other column. I don't intend to keep lvi if I don't need it. Either way I need to get entry.key displayed in the first column and entry.value in the second. Any ideas?


Solution

  • I don't know what the SchemaManager is so I just used a Hashtable with some dummy data to get an enumerator with some dictionary entries. Below sample code uses a regular WPF listview with two columns with no formatting to keep it simple.

    XAML:

    <Window x:Class="TwoColsDemo.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
    
        <ListView ItemsSource="{Binding Path=Data}">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Key" DisplayMemberBinding="{Binding Path=Key}" />
                        <GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=Value}" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>
    </Window>
    

    Code behind:

    using System.Collections;
    using System.Collections.Generic;
    using System.Windows;
    
    namespace TwoColsDemo
    {
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
    
                Hashtable sman = new Hashtable();
                sman.Add("Key1", "Value1");
                sman.Add("Key2", "Value2");
    
                Data = new List<DictionaryEntry>();
    
                IEnumerator enumerator = sman.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DictionaryEntry entry = (DictionaryEntry)enumerator.Current;
                    Data.Add(entry);
                }
    
                DataContext = this;
            }
    
            public List<DictionaryEntry> Data { get; private set; }
        }
    }