2014-03-03 184 views
0

我有一個問題,我看不出爲什麼它不工作。 我有一個3級的Treeview與轉換器,讓我倒過來的兩個第一級。 在這個TreeView中,我試圖使用IsSelected屬性,但出於某種原因,我不明白它永遠不會在ViewModel中調用。WPF MVVM TreeView IsSelected綁定不工作

這裏是xaml。

<TreeView Margin="0,0,0,0" Padding="0,7,0,7" BorderBrush="{x:Null}" Background="#FFF8FAFB" Name="BusinessCaseTreeView"> 
    <TreeView.ItemContainerStyle> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
     </Style> 
    </TreeView.ItemContainerStyle> 
    <TreeView.ItemsSource> 
     <MultiBinding Converter="{converter:BusinessCasesConverter}"> 
      <Binding Path="BusinessCases"></Binding> 
      <Binding Path="GroupByCreditor"></Binding> 
     </MultiBinding> 
    </TreeView.ItemsSource> 
    <TreeView.Resources> 
     <HierarchicalDataTemplate DataType="{x:Type converter:BusinessCaseTreeViewableFirst}" ItemsSource="{Binding SecondLevel}"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding FirstLevel.Id}" Margin="10,0,0,0" Foreground="#FF03577A" FontWeight="Bold" /> 
       <TextBlock Text="-" Margin="5,0,0,0" Foreground="#FF03577A" FontWeight="Bold"/> 
       <TextBlock Text="{Binding FirstLevel.FirstName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF03577A" /> 
       <TextBlock Text="{Binding FirstLevel.LastName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF03577A" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
     <HierarchicalDataTemplate DataType="{x:Type converter:BusinessCaseTreeViewModelSecond}" ItemsSource="{Binding BusinessCases}"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding SecondLevel.Id}" Margin="10,0,0,0" Foreground="#FF095A7C" FontWeight="Bold" /> 
       <TextBlock Text="-" Margin="5,0,0,0" Foreground="#FF095A7C" FontWeight="Bold"/> 
       <TextBlock Text="{Binding SecondLevel.FirstName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF095A7C" /> 
       <TextBlock Text="{Binding SecondLevel.LastName}" Margin="5,0,0,0" FontWeight="Bold" Foreground="#FF095A7C" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
     <DataTemplate DataType="{x:Type business:BusinessCase}"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="(" Margin="10,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold" /> 
       <TextBlock Text="{Binding Id}" Margin="1,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold" /> 
       <TextBlock Text=")" Margin="2,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold"/> 
       <TextBlock Text="{Binding CreationDate, StringFormat='{}{0:dd MMMM yyyy}'}" Margin="5,0,0,0" Foreground="#FF0B89BD" /> 
       <TextBlock Text="-" Margin="5,0,0,0" Foreground="#FF0B89BD" FontWeight="Bold"/> 
       <TextBlock Text="{Binding ClosureDate, StringFormat={}{0:dd MMMM yyyy}}" Margin="5,0,0,0" Foreground="#FF0B89BD" /> 
      </StackPanel> 
     </DataTemplate> 
    </TreeView.Resources> 
</TreeView> 

我永遠不會到達我的ViewModel中的IsSelected setter。如果你有任何想法,歡迎。

在此先感謝

+0

有沒有在你的輸出窗口任何有約束力的錯誤? – Matt

回答

1

它可能有一些與你正在做它ItemContainerStyle的事實。此外,還要確保IsSelected是在所有類型的被添加到TreeView

試試這個:

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