Click here to Skip to main content
15,900,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I would like to know that how Tree View Items get read only in WPF. Is read only property there?
I have a Treeview which is expanded and I want to make user not allow to collapse it.How I do it.
<treeview> <treeview.itemcontainerstyle> <Style TargetType="TreeViewItem">
<setter property="IsSelected" value="false">
</Style>
I used this in Xaml.But still allow to collapse it.
Please help me.

Thanks & Regards
Monish K.M
Posted
Updated 27-Jun-12 21:35pm
v3

No. If you mean the class System.Windows.Controls.TreeViewItem, it does not have anything like that, please see by yourself: http://msdn.microsoft.com/en-us/library/system.windows.controls.treeviewitem%28v=vs.95%29.aspx[^].

The reason for this is not simple, but very simple, thing about it: the whole control System.Windows.Controls.TreeView is already totally read-only in the following sense: there are no predefined modifying operations which could be immediately used via UI, unless you define this in your code. Moreover, this is so because this control does not contain any predefined data object, unless the developer defines them. Anything like "edit mode" (in-place editing) or other data modification operations (add/remove/edit a node) via UI are possible only if you defined them in your code. If you do, you will be able to define your own notion of read-only or anything like that. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.treeview%28v=vs.95%29.aspx[^].

—SA
 
Share this answer
 
v3
If you use the MVVM pattern, you can bind to the IsExpanded, and always return true. The other possiblility to so used the Collapsed event on the TreeViewItem to force the item back to expanded.
 
Share this answer
 
Does this help:
XML
<TreeView>
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Setter Property="IsSelected" Value="false"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>


This will make your treeview items not selectable.
 
Share this answer
 
Comments
Lee_Olsen 21-Feb-13 16:29pm    
This only makes the TreeViewItems default value of IsSelected be set to false. You can still select an item. You would need to bind IsSelected to a property that always returns false to make it unselectable.
Sandeep Mewara 22-Feb-13 0:32am    
Agree with you. But I see question modified. Not sure what was Op's real intention then. But, yes, answer given by me sets false for first time. If it has to be throughout, something like you suggested should be done.
Lee_Olsen 21-Feb-13 16:34pm    
Another option besides binding IsSelected to a property always returning false since this isn't always an option.

<TreeView>
<TreeView.ItemContainerStyle>

<Style TargetType="TreeViewItem">

<Setter Property="Focusable" Value="False" />

</Style>
</TreeView.ItemContainerStyle>
</TreeView>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900