views:

117

answers:

1

I've got a MVVM WPF app with the TreeView databound to a viewmodel class. It is essentially a file explorer. I want to add the ability to "Add a new folder" to the hierarchy. To achieve the desired functionality what I am trying to do is simply switch the Textblock out for an editable TextBox in my datatemplate. This is what my datatemplate looks like:

<TreeView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                <StackPanel Name="tv_itempanel"
                            Orientation="Horizontal" 
                            Margin="2">
                    <Image Source="{Binding Icon}" Margin="4"/>
                    <TextBlock Name="treeitem_tblock" Margin="4" Text="{Binding Name}"/>
                    <TextBox Width="200" Visibility="Collapsed" Name="treeitem_tbox"/>
                </StackPanel>
            </HierarchicalDataTemplate>
</TreeView.ItemTemplate>

The problem is that I cannot modify an individual TreeViewItem since the treeview is databound. Any ideas? Thanks

+3  A: 

Add a bool IsEditable property to your VM objects, and bind the visibility of the TextBox to is (using a converter to transform the boolean value to a Visibility enum). That way you don't need to manipulate the TreeViewItem directly, simply mark the data object as editable, and it will flow naturally to your view.

Aviad P.
Thanks, I had a friend tell me this method just a few minutes ago. It works perfectly for what I'm trying to do.
Seabass__