2011-11-06 29 views
0

我有一個TreeView的幾個同步實例..TreeView模板的瘋狂

它工作得很好,我綁定了一些屬性到ViewModel。

<Style x:Key="FlattenedTreeViewItem" TargetType="{x:Type TreeViewItem}"> 

    <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
    <Setter Property="IsExpanded" Value="{Binding IsExpanded}" /> 
    <Setter Property="IsSelected" Value="{Binding IsSelected}" /> 

由於某種原因,我決定重寫TreeViewItem的模板。

這個刪除上述

所有我想要做的屬性綁定,是帶回默認選擇回樹型視圖,但似乎沒有任何工作。 我嘗試在IsSelected上使用簡單的觸發器並設置邊框/背景/前景。沒有任何顯示任何更改。

我已經搜索了,到目前爲止我發現的任何東西都是XAML頁面,只是爲了設置Selected style? 是不是有一種簡單的方法將它帶回模板?

同樣適用於IsExapnded, 我必須這樣做:

<VisualStateManager.VisualStateGroups> 
    <VisualStateGroup x:Name="ExpansionStates"> 
     <VisualState x:Name="Expanded"> 
      <Storyboard> 
       <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ItemsHost"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" /> 
       </ObjectAnimationUsingKeyFrames> 
      </Storyboard> 
     </VisualState> 
     <VisualState x:Name="Collapsed" /> 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 

似乎過高,但至少這一個工程。

隨着選擇,我不能讓它使用簡單的觸發器工作..我不能把選擇的樣式,我不想帶來一些巨大的醜塊大小xaml。

這裏是我完整的模板:

<Setter Property="Template"> 
     <Setter.Value> 

      <ControlTemplate TargetType="TreeViewItem"> 

       <StackPanel> 
       <VisualStateManager.VisualStateGroups> 
        <VisualStateGroup x:Name="ExpansionStates"> 
         <VisualState x:Name="Expanded"> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ItemsHost"> 
            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" /> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </VisualState> 
         <VisualState x:Name="Collapsed" /> 
        </VisualStateGroup> 
       </VisualStateManager.VisualStateGroups> 
        <ContentPresenter ContentSource="Header" /> 
        <ItemsPresenter Name="ItemsHost" Visibility="Collapsed" /> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 

回答

1

最短(也不太漂亮)的路線:用邊框包圍StackPanel,並將其背景改爲Trigger

的XAML:

<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="TreeViewItem"> 
      <Border x:Name="Bd" Background="Transparent"> 
      <StackPanel> 
      <VisualStateManager.VisualStateGroups> 
       <VisualStateGroup x:Name="ExpansionStates"> 
        <VisualState x:Name="Expanded"> 
         <Storyboard> 
          <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ItemsHost"> 
           <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}" /> 
          </ObjectAnimationUsingKeyFrames> 
         </Storyboard> 
        </VisualState> 
        <VisualState x:Name="Collapsed" /> 
       </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
       <ContentPresenter ContentSource="Header" /> 
       <ItemsPresenter Name="ItemsHost" Visibility="Collapsed" /> 
      </StackPanel> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter TargetName="Bd" Property="Background" Value="HotPink"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 

注: - 變 「暗粉紅」 到想要的顏色

注2: - 對於一個樹型視圖功能,你最好添加一個ToggleButton,這將允許用戶展開\摺疊該項目。它的最小工作量(堆棧面板中的某處):

<ToggleButton IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/> 

P.S. - 等到你在Luna主題中看到TabItem的模板....

+0

謝謝!我最終使用表達式混合對模板進行了逆向工程,發現我在內容展示器周圍缺少邊框。再次感謝 –

2

模板是「XAML的巨大丑陋塊」,這是現實,什麼你可以做的。

+0

好的,選擇回來的最好方法是什麼? –

+0

@SonicSoul:選擇應該已經存在,只需要使用一些觸發器將容器的背景和前景設置爲突出顯示的顏色即可。只需從默認模板複製那些東西。 –

+0

我如何獲得默認模板的標記?我沒有表情混合.. –