2012-06-14 116 views
0

我有以下代碼。treeview和mvvm我不填充樹視圖

我的樹型視圖:

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; } 
    } 

} 

我在我的樹視圖中的視圖的視圖模型。我在GUI中有更多的元素,還有一些底部等等,但我只把與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); 
} 

最後我的觀點的XAML:

<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> 

的問題是,當我稱之爲componenteJerarquiaConstruirArbol方法,不填充樹視圖。

我認爲問題是綁定,但我看不到問題。

回答

1

屬性ComponenteJerarquia只是一個普通的屬性,所以當你設置它時,綁定系統不會被通知它需要更新綁定。您需要在該課程中實施INotifyPropertyChanged並在籌備者中提升PropertyChanged事件。

好的,另一部分是你不應該把你的HierarchicalDataTemplates放到你的TreeView中 - 這使得它認爲你正在試圖把它們放在實際的樹中。相反,將這些模板移到您的資源中。

+0

我實現了INotifyPropertyChanged,但是現在我得到了另一個錯誤,那就是:在使用ItemSource之前,集合必須是空的。 –

+0

編輯瞭解更多信息。 – Tim

+0

您爲GTS.CMMS.Client.Views.ucMaquinasPrincipalView的ViewModel實現了INotifyPropertyChanged?我看到你有ComponenteNodoViewModel,但你沒有發佈整個其他課程。此外,List <>不會實現ICollectionChanged,如果您想進行更改並自動更新,那麼這將是必需的。也許使用ObservableCollection <>。 –