FlyingStreudel
FlyingStreudel

Reputation: 4464

WPF Databinding Treeview To List Weirdness

So I am having a weird issue with databinding. I am attempting to databind a List to a treeview and for some reason I can select items that are created as part of the binding.

Binding Source:

<TreeView Name="paramTree" 
          BorderBrush="DarkSlateGray"
          Margin="0,0,0,1">
    <TreeViewItem Name="systemNode"
                  Header="System Info"
                  ItemsSource="{Binding}">
        <TreeViewItem.ItemTemplate>
            <DataTemplate>
                <TreeViewItem Header="{Binding}"/>
            </DataTemplate>
        </TreeViewItem.ItemTemplate>
    </TreeViewItem>
</TreeView>

Code Behind:

private PropertyList sysList = new PropertyList(typeof(System.Environment));

public MainWindow()
{
    InitializeComponent();
    .
    .
    systemNode.DataContext = sysList;
}

class PropertyList : List<string>
{
    public PropertyList(Type t)
    {
        // Get properties of this type
        PropertyInfo[] propertyInfo = t.GetProperties();

        foreach (PropertyInfo property in propertyInfo)
        {
            Add(property.Name);
        }
    }
}

This creates a subtree of the "System Info" node with all of the properties of the System.Environment, but I can't click on any of the new TreeViewItems... Help?

Upvotes: 0

Views: 566

Answers (1)

brunnerh
brunnerh

Reputation: 185072

Just from looking at this i assume it's that old problem, correct me if i'm wrong...

The TreeView generates a TreeViewItem around your items automatically. Your ItemTemplate should be:

    <TreeViewItem.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
        </DataTemplate>
    </TreeViewItem.ItemTemplate>

Upvotes: 2

Related Questions