开发者

Modifying an individual TreeViewItem on a databound WPF TreeView without modifying the data source

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>
            </HierarchicalDataTe开发者_运维百科mplate>
</TreeView.ItemTemplate>

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜