2016-07-26 5 views
0

私は、ツリービューをParentViewModelのObservableCollectionにバインドしました。ツリービューは異なるビューモデルで選択されています

ParentViewModelには、ChildViewModelのObservableCollectionが含まれています。

この両方のViewModelは、IsSelectedプロパティを実装しています。実装は少し違うので、私は両方の中に入れています。親子アイテムを押すと、プログラムの動作が異なる必要があります。

私の問題は、Childアイテムを選択した場合でも、ChildViewModelのものではなく、ParentViewModelのIsSelectedプロパティを実行することです。

これは私のXAMLです:

  <TreeView Name="MyTreeView" 
     ItemsSource="{Binding ParentList}"> 

     <TreeView.ItemContainerStyle> 
      <Style TargetType="{x:Type TreeViewItem}"> 
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
      </Style> 
     </TreeView.ItemContainerStyle> 

     <TreeView.Resources> 
      <HierarchicalDataTemplate DataType="{x:Type local:ParentViewModel}" ItemsSource="{Binding Childs}"> 

       <Grid > 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto"/> 
        <ColumnDefinition Width="1*"/> 
       </Grid.ColumnDefinitions> 

       <Image Grid.Column="0" Source="{Binding PictureString}" Height="32" Width="32" Margin="0,8,6,4" /> 
       <TextBlock Grid.Column="1" Text="{Binding Name}" FontSize="15" Margin="10" /> 

       </Grid> 

       <HierarchicalDataTemplate.ItemContainerStyle> 
       <Style TargetType="{x:Type TreeViewItem}"> 
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
       </Style> 
       </HierarchicalDataTemplate.ItemContainerStyle> 

      </HierarchicalDataTemplate> 

      <DataTemplate DataType="{x:Type local:ChildViewModel}"> 
       <Grid > 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto"/> 
        <ColumnDefinition Width="1*"/> 
       </Grid.ColumnDefinitions> 

       <!--<Image Grid.Column="0" Source="{Binding PictureString}" Height="32" Margin="0,8,6,4" />--> 
       <TextBlock Grid.Column="1" Text="{Binding Name}" Height="18" FontSize="15" Margin="10"/> 

       </Grid> 
      </DataTemplate> 

     </TreeView.Resources> 

    </TreeView> 

プログラムはChildViewModelのIsSelectedセッターを打つことはありません。事前に

おかげ

EDIT

これはBaseViewModelです:

Public Class InheritableTreeViewItem 
    Implements INotifyPropertyChanged 

    Friend m_Name As String 
    Public Property Name As String 
     Get 
     Return m_Name 
     End Get 
     Set(value As String) 
     If (value <> m_Name) Then 
      m_Name = value 
      NotifyPropertyChanged("Name") 
     End If 
     End Set 
    End Property 

    Friend m_IsSelected As Boolean 
    Public Overridable Property IsSelected As Boolean 
     Get 
     Return m_IsSelected 
     End Get 
     Set(value As Boolean) 
     If (value <> m_IsSelected) Then 
      m_IsSelected = value 
      NotifyPropertyChanged("IsSelected") 
     End If 
     End Set 
    End Property 


    Private m_sPictureString As String 
    Private m_Items As ObservableCollection(Of InheritableTreeViewItem) 

    Public Property PictureString As String 
     Get 
     Return m_sPictureString 
     End Get 
     Set(value As String) 
     If value <> m_sPictureString Then 
      m_sPictureString = value 
      NotifyPropertyChanged("PictureString") 
     End If 
     End Set 
    End Property 

    Public Property Items As ObservableCollection(Of InheritableTreeViewItem) 
     Get 
     Return m_Items 
     End Get 
     Set(value As ObservableCollection(Of InheritableTreeViewItem)) 
     m_Items = value 
     End Set 
    End Property 

    Sub New(Name As String) 
     Me.Name = Name 
     Me.Items = New ObservableCollection(Of InheritableTreeViewItem) 
    End Sub 

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged 

    Public Sub NotifyPropertyChanged(propName As String) 
     RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName)) 
    End Sub 


End Class 

そして、これがParentViewModelです:ChildViewModelは基本的に同じである

Public Class ParentViewModel 
    Inherits InheritableTreeViewItem 

    Public Overrides Property IsSelected As Boolean 
     Get 
     Return m_IsSelected 
     End Get 
     Set(value As Boolean) 
     If (value <> m_IsSelected) Then 
      m_IsSelected = value 
      If value Then 
       ' The function that I want to call When Parent item is selected in the tree 
      End If 
      NotifyPropertyChanged("IsSelected") 
     End If 
     End Set 
    End Property 

    Sub New(Name As String) 
     MyBase.New(Name) 
     Me.PictureString = "/Resources/TreeView/Folder.png" 
    End Sub 

End Class 

、それが唯一の違いそれが選択されたときに呼び出す関数用です。この関数は、一部のテキストボックスの内容と可視性を変更します。

+0

ポストビューモデル。 –

+0

私はそれを投稿しました。しかし、私は、すべてのtreeview要素が正しく表示されているので、エラーはXAMLバインディングにあると考えます。 – Emmanuele

答えて

0

私はあなたの問題は、このブロックであると思う:

<TreeView.ItemContainerStyle> 
    <Style TargetType="{x:Type TreeViewItem}"> 
     <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
    </Style> 
</TreeView.ItemContainerStyle> 

それがスタイルですので、私はDataContextのが正しく継承されていることはよく分かりません。また、なぜこれをHierarchicalDataTemplateで再定義しますか? ChildViewModelItemsのこのスタイルを再定義する必要はないと思います。

おそらく、あなたのデータバインドをデバッグして、このプロパティで何が起きているのかを確認することができます:How to debug data binding

また、チャイルドではなくチャイルドである複数のチャイルド)

関連する問題