2012-05-30 30 views
0

我想從radtreeview採取高光背景。我創建了一個樣式來做到這一點,但我一直在收到錯誤和異常,例如「Items collection must be empty」。如果我註釋掉應用程序的風格,那麼我知道它是問題的原因。我對WPF相當陌生,我確信我只是不明白如何使用樣式。 感謝您的幫助。這是代碼。我需要幫助實現一種風格到我的radTreeView

<Grid x:Name="LayoutRoot" Background="Salmon"> 

     <telerik:RadTreeView x:Name="radTreeView" Margin="8" ItemsSource="{Binding Errors}" Background="Salmon" Style="{StaticResource treeStyle}"> 
      <Style TargetType="{x:Type telerik:RadTreeViewItem}" x:Name="treeStyle"> 
       <Setter Property="Focusable" Value="False"/> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="true"> 
         <Setter Property="Background" Value="{x:Null}"/> 
         <Setter Property="BorderBrush" Value="{x:Null}"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 


      <telerik:RadTreeView.ItemTemplate> 
       <HierarchicalDataTemplate ItemsSource="{Binding SubItems}" > 
        <Grid Background="Salmon"> 
         <Grid.RowDefinitions> 
          <RowDefinition/> 
          <RowDefinition/> 

         </Grid.RowDefinitions> 
         <TextBlock Text="{Binding Description}" IsHitTestVisible="False" /> 

         <ListBox Grid.Row="1" ItemsSource="{Binding Messages}" Margin="20,0,0,0" BorderBrush="#00000000" BorderThickness="0" Background="Salmon" IsHitTestVisible="False" > 
          <ListBox.ItemTemplate> 
           <DataTemplate> 
            <TextBlock Text="{Binding Message}"/> 
           </DataTemplate> 
          </ListBox.ItemTemplate> 
         </ListBox> 
        </Grid> 

       </HierarchicalDataTemplate> 


      </telerik:RadTreeView.ItemTemplate> 


     </telerik:RadTreeView> 

    </Grid> 

</UserControl> 

如果你知道這是行不通的,我也試圖擺脫與代碼風格的亮點:

<Style TargetType="TreeViewItem"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFF"/> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="#000"/> 
    </Style.Resources> 
</Style> 

回答

2

你得到的例外,因爲你的風格標籤實際上是一個是項目在樹中,並且您有ItemsSource集合。

<telerik:RadTreeView.ItemContainerStyle>標記圍繞樣式。

這應該解決異常,但它不會給你預期的結果,因爲樹視圖項目的控件模板實際上顯示了另一個不受Background屬性影響的邊框。您將需要更改控制模板。

Telerik改變版本之間的風格,所以給你一個錯誤版本的模板可能不會幫助你。

但是,您可以轉到Telerik的安裝文件夾並查找名爲「Themes」的文件夾。在那裏你會找到一個包含telerik所有主題的解決方案。

  • 選擇一個您使用的。
  • 查找樹視圖的資源字典,並將項目的樣式和模板複製到項目中。
  • 更改xmlns定義,確保您擁有該樣式所依賴的所有畫筆和資源。
  • 運行以查看風格是否正常。
  • 在模板中,找到VisualStatex:Name="MouseOver"並刪除其中的故事板。
+0

一旦我修改它,它不會讓我保存文件。是否真的沒有其他方式來做到這一點?這太瘋狂了.. – JLott

+0

對不起,錯過了這個評論 - 這可能是因爲該文件位於Telerik的TFS源代碼控制下,並且在將文件添加到安裝文件夾之前,它們不會從文件中刪除只讀標誌。轉到文件的屬性並刪除只讀標誌。 – XAMeLi