Reputation: 19396
I have the following code.
My TreeViewItem:
class ComponenteNodoViewModel : BaseViewModel
{
public string Nombre { get; set; }
private List<ComponenteNodoViewModel> _children;
private bool _isExpanded;
private bool _isSelected;
public ComponenteNodoViewModel Parent { get; set; }
public bool IsExpanded
{
get { return _isExpanded; }
set
{
_isExpanded = value;
base.RaisePropertyChangedEvent("IsExpanded");
}
}
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
base.RaisePropertyChangedEvent("IsSelected");
}
}
public List<ComponenteNodoViewModel> Children
{
get { return _children; }
set { _children = value; }
}
}
My ViewModel of the view in which I have the treeView. I have more elements in the GUI, also some bottoms and so on, but I only put the code that has relationship with the treeView.
public List<ComponenteNodoViewModel> ComponenteJerarquia { get; private set; }
...
private void componenteJerarquiaConstruirArbol(List<vComponentesEstructuras> paramNodos)
{
List<ComponenteNodoViewModel> misNodos = new List<ComponenteNodoViewModel>();
ComponenteNodoViewModel miNodo = new ComponenteNodoViewModel();
miNodo.Nombre = "Prueba";
misNodos.Add(miNodo);
ComponenteJerarquia = new List<ComponenteNodoViewModel>(misNodos);
}
And finally my xaml of the view:
<UserControl x:Class="GTS.CMMS.Client.Views.ucMaquinasPrincipalView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
xmlns:ViewModels="clr-namespace:Project.ViewModel"
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="1000">
<Grid>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="232" />
<ColumnDefinition Width="757" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="309*" />
<RowDefinition Height="350*" />
</Grid.RowDefinitions>
<TreeView ItemsSource="{Binding ComponenteJerarquia}" Margin="6,6,8,5" Name="trvComponente" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<HierarchicalDataTemplate DataType="{x:Type ViewModels:ComponenteNodoViewModel}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Nombre}"/>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:ComponenteNodoViewModel}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Nombre}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView>
</Grid>
</Grid>
</UserControl>
The problem is that when I call the componenteJerarquiaConstruirArbol method, the treeview is not populated.
I think that the problem is the binding, but I can't see the problem.
Upvotes: 0
Views: 325
Reputation: 15247
The propperty ComponenteJerarquia
is just a plain property, so when you set it, the binding system isn't notified that it needs to update the binding. You need to implement INotifyPropertyChanged
in that class and raise the PropertyChanged
event in the setter.
OK, the other part of this is that you shouldn't be putting your HierarchicalDataTemplates inside your TreeView - that's making it think you're trying to put those in the actual tree. Move those templates up to your Resources instead.
Upvotes: 1