2011-01-21 87 views
0

我有一個TreeView,其中每個節點都有一個圖標和一個描述性文本。但我不希望任何節點可以被選中。相反,我希望每個節點充當按鈕。它在用戶按下時運行一個命令。但它可能不像一個按鈕或超鏈接帶按鈕的TreeView

這是我到目前爲止所嘗試的。問題在於文本是藍色的,文本是下劃線的。另外,有時節點被選中,因此是藍色的。

<TreeView.Resources> 
    <HierarchicalDataTemplate DataType="{x:Type vm:ListGroupViewModel}" ItemsSource="{Binding Children}"> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Text}" FontWeight="Bold" ></TextBlock> 
     </StackPanel> 
    </HierarchicalDataTemplate> 

    <DataTemplate DataType="{x:Type vm:ListNodeViewModel}"> 
     <TextBlock> 
      <Hyperlink TextDecorations="{x:Null}" Command="{Binding ClickCommand, Mode=OneTime}"> 
       <StackPanel Orientation="Horizontal"> 
        <Image Margin="0,2,2,0" Source="{Binding Icon}" /> 
        <TextBlock Text="{Binding Text}" /> 
       </StackPanel> 
      </Hyperlink> 
     </TextBlock> 
    </DataTemplate> 
</TreeView.Resources> 

回答

3

您應該重寫超鏈接樣式:

 <Style x:Key="HyperlinkStyle" TargetType="Hyperlink"> 
      <Setter Property="Foreground" 
        Value="Black"/> 
      <Setter Property="TextDecorations" 
        Value="{x:Null}"/> 
     </Style> 

     <DataTemplate DataType="{x:Type vm:ListNodeViewModel}"> 
      <TextBlock> 
       <Hyperlink Command="{Binding ClickCommand, Mode=OneTime}" 
          Style="{StaticResource HyperlinkStyle}"> 
        <StackPanel Orientation="Horizontal"> 
         <Image Margin="0,2,2,0" Source="{Binding Icon}" /> 
         <TextBlock Text="{Binding Name}" /> 
        </StackPanel> 
       </Hyperlink> 
      </TextBlock> 
     </DataTemplate> 

隱藏樹項目的選擇,您可以覆蓋SystemColors.HighlightBrushKey,該樹視圖使用要突出項目:

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
+0

太棒了。工作得很好......除了一個細節。選定的組具有通常藍色背景和白色前景。現在背景將保持白色,但文字會變爲白色,這意味着文字在選中時會消失。 Lyssna Lösfonetiskt Ordbok - Visa detaljerad ordbokpronomen0.its0.your0.their0.his0.her – magol 2011-01-21 12:51:03

0

樹視圖項目的分層DataTemplate應該做的伎倆。告訴我們你的嘗試。

+0

我知道這個數據模板,但不知道我會怎麼做到的。我已經添加了我到目前爲止的代碼 – magol 2011-01-21 09:22:50