I have a treeview that is bound to a class:
The problem is that I want to add a property that the treeviewitem have, to activate or deactivate parts of the tree, but when I bind an own class, I don't know how to add that property.
Try:
- derive my class from treeviewitem (the bind didn't work for me, maybe I did that wrong)
- add an enable property (it works, but I don't know what to do with that property).
- transform the items of the tree to treeviewitem (it did not work, they are not of that type).
So how do I tell the tree that certain items are disabled (graying them, underlining them, it doesn't matter how, but how so that the user can't click on them).
Tree definition (xalm)
<TreeView x:Name="treeView2" >
<TreeView.DataContext>
<Controles:ListaArbol/>
</TreeView.DataContext>
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type Controles:ListaArbol}" >
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Imagen}" Stretch="Fill" Width="30" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Text="{Binding texto}" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
Class that is bound:
public class ListaArbol: TreeViewItemBase
{
public ListaArbol()
{
this.Children = new ObservableCollection<ListaArbol>();
}
public string texto { get; set; }
public string Imagen { get; set; }
public string Control { get; set; }
public ObservableCollection<ListaArbol> Children { get; set; }
}
The above class derives from:
public class TreeViewItemBase : INotifyPropertyChanged
{
.....
}
and to load the tree I do something like this:
List<ListaArbol> items = new List<ListaArbol>();
ListaArbol i1 = new ListaArbol { texto = "Archivos", Imagen = "Imagenes/blue-folder-vector.png", Control="ControlArchivos" };
ListaArbol i2 = new ListaArbol { texto = "Consultas", Imagen = "Imagenes/consultas.jpg", Control= "ControlConsultas" };
items.Add(i1);
items.Add(i2);
treeView2.ItemsSource = items;
The first thing you should do is add a property to your class
ListaArbol
to mark if the node is activated or not, for exampleIsEnabled
:In your XAML you must add a
Setter
"bindeado" to said property insideTreeView.ItemContainerStyle
:Lastly, when initializing your items
ListaArbol
, set that property totrue
ofalse
if you want to turn it on or off:In this example, the first node will be up and the second will be down.