treeview binded in wpf disable nodes

1

I have a treeview that is binded to a class:

The problem is that I want to add a property that if they have the treeviewitem, to activate or deactivate parts of the tree, but when bindear a class of its own, I do not know how to add that property.

Probe:

  • derive my class from treeviewitem (I did not stick the bind, eye maybe I did that wrong)
  • add an enable property (go, but I do not know what to do with that property).
  • transform tree items to treeviewitem (did not walk, are not of that type).

Then how do I tell the tree that certain items are deactivated (grisandolos, underlining, no matter how, but so that the user can not click on them).

Definition of the tree (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 binded:

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 previous class is derived 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;
    
asked by gbianchi 06.09.2017 в 19:33
source

1 answer

2

The first thing you should do is add a property to your class ListaArbol to mark if node is activated or not, for example IsEnabled :

...
public string texto { get; set; }
public string Imagen { get; set; }
public string Control { get; set; }
public bool IsEnabled { get; set; }

In your XAML you must add a Setter "binded" to that property within TreeView.ItemContainerStyle :

<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.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

Finally, when initializing the items of your ListaArbol , put that property to true or false if you want to activate or deactivate it:

ListaArbol i1 = new ListaArbol { texto = "Archivos", Imagen = "Imagenes/blue-folder-vector.png",
                                Control = "ControlArchivos",IsEnabled=true };
ListaArbol i2 = new ListaArbol { texto = "Consultas", Imagen = "Imagenes/consultas.jpg", 
                                 Control = "ControlConsultas" ,IsEnabled=false};

In this example, the first node will be active and the second will be inactive.

    
answered by 07.09.2017 / 10:02
source