Guest User

Untitled

a guest
Jun 25th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.40 KB | None | 0 0
  1. <ControlTemplate TargetType="{x:Type TreeViewItem}">
  2. <ControlTemplate.Resources>
  3. <local:LeftMarginMultiplierConverter Length="19" x:Key="lengthConverter" />
  4. </ControlTemplate.Resources>
  5. <StackPanel>
  6. <Border Name="Bd"
  7. Background="{TemplateBinding Background}"
  8. BorderBrush="{TemplateBinding BorderBrush}"
  9. BorderThickness="{TemplateBinding BorderThickness}"
  10. Padding="{TemplateBinding Padding}">
  11. <Grid Margin="{Binding Converter={StaticResource lengthConverter},
  12. RelativeSource={RelativeSource TemplatedParent}}">
  13.  
  14. <Grid.ColumnDefinitions>
  15. <ColumnDefinition Width="19" />
  16. <ColumnDefinition />
  17. </Grid.ColumnDefinitions>
  18. <ToggleButton x:Name="Expander"
  19. Style="{StaticResource ExpandCollapseToggleStyle}"
  20. IsChecked="{Binding Path=IsExpanded,
  21. RelativeSource={RelativeSource TemplatedParent}}"
  22. ClickMode="Press"/>
  23.  
  24. <ContentPresenter x:Name="PART_Header"
  25. Grid.Column="1"
  26. ContentSource="Header"
  27. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
  28. </Grid>
  29. </Border>
  30. <ItemsPresenter x:Name="ItemsHost" />
  31. </StackPanel>
  32. <!-- Triggers -->
  33. </ControlTemplate>
  34.  
  35. public static class TreeViewItemExtensions
  36. {
  37. public static int GetDepth(this TreeViewItem item)
  38. {
  39. FrameworkElement elem = item;
  40. while (elem.Parent != null)
  41. {
  42. var tvi = elem.Parent as TreeViewItem;
  43. if (tvi != null)
  44. return tvi.GetDepth() + 1;
  45. elem = elem.Parent as FrameworkElement;
  46. }
  47. return 0;
  48. }
  49. }
  50.  
  51. public class LeftMarginMultiplierConverter : IValueConverter
  52. {
  53. public double Length { get; set; }
  54.  
  55. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  56. {
  57. var item = value as TreeViewItem;
  58. if (item == null)
  59. return new Thickness(0);
  60.  
  61. return new Thickness(Length * item.GetDepth(), 0, 0, 0);
  62. }
  63.  
  64. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  65. {
  66. throw new System.NotImplementedException();
  67. }
  68. }
  69.  
  70. <TreeView Margin="50" HorizontalContentAlignment="Stretch">
  71. <TreeViewItem Header="test2"/>
  72. <TreeViewItem Header="test2">
  73. <TreeViewItem Header="sub test">
  74. <TreeViewItem Header="sub test1-1"/>
  75. <TreeViewItem Header="sub test1-2"/>
  76. </TreeViewItem>
  77. <TreeViewItem Header="sub test2"/>
  78. </TreeViewItem>
  79. <TreeViewItem Header="test3"/>
  80. </TreeView>
  81.  
  82. <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
  83.  
  84. <!--=================================================================
  85. TreeViewItem
  86. ==================================================================-->
  87. <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
  88. <Setter Property="Focusable" Value="False"/>
  89. <Setter Property="Template">
  90. <Setter.Value>
  91. <ControlTemplate TargetType="ToggleButton">
  92. <Grid
  93. Width="15"
  94. Height="13"
  95. Background="Transparent">
  96. <Path x:Name="ExpandPath"
  97. HorizontalAlignment="Left"
  98. VerticalAlignment="Center"
  99. Margin="1,1,1,1"
  100. Fill="{StaticResource GlyphBrush}"
  101. Data="M 4 0 L 8 4 L 4 8 Z"/>
  102. </Grid>
  103. <ControlTemplate.Triggers>
  104. <Trigger Property="IsChecked"
  105. Value="True">
  106. <Setter Property="Data"
  107. TargetName="ExpandPath"
  108. Value="M 0 4 L 8 4 L 4 8 Z"/>
  109. </Trigger>
  110. </ControlTemplate.Triggers>
  111. </ControlTemplate>
  112. </Setter.Value>
  113. </Setter>
  114. </Style>
  115. <Style x:Key="TreeViewItemFocusVisual">
  116. <Setter Property="Control.Template">
  117. <Setter.Value>
  118. <ControlTemplate>
  119. <Border>
  120. <Rectangle Margin="0,0,0,0"
  121. StrokeThickness="5"
  122. Stroke="Black"
  123. StrokeDashArray="1 2"
  124. Opacity="0"/>
  125. </Border>
  126. </ControlTemplate>
  127. </Setter.Value>
  128. </Setter>
  129. </Style>
  130.  
  131.  
  132. <Style x:Key="{x:Type TreeViewItem}"
  133. TargetType="{x:Type TreeViewItem}">
  134. <Setter Property="Background"
  135. Value="Transparent"/>
  136. <Setter Property="HorizontalContentAlignment"
  137. Value="{Binding Path=HorizontalContentAlignment,
  138. RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  139. <Setter Property="VerticalContentAlignment"
  140. Value="{Binding Path=VerticalContentAlignment,
  141. RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  142. <Setter Property="Padding"
  143. Value="1,0,0,0"/>
  144. <Setter Property="Foreground"
  145. Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  146. <Setter Property="FocusVisualStyle"
  147. Value="{StaticResource TreeViewItemFocusVisual}"/>
  148. <Setter Property="Template">
  149. <Setter.Value>
  150. <ControlTemplate TargetType="{x:Type TreeViewItem}">
  151. <ControlTemplate.Resources>
  152. <local:LeftMarginMultiplierConverter Length="19" x:Key="lengthConverter" />
  153. </ControlTemplate.Resources>
  154. <StackPanel>
  155. <Border Name="Bd"
  156. Background="{TemplateBinding Background}"
  157. BorderBrush="{TemplateBinding BorderBrush}"
  158. BorderThickness="{TemplateBinding BorderThickness}"
  159. Padding="{TemplateBinding Padding}">
  160. <Grid Margin="{Binding Converter={StaticResource lengthConverter},
  161. RelativeSource={RelativeSource TemplatedParent}}">
  162. <Grid.ColumnDefinitions>
  163. <ColumnDefinition Width="19" />
  164. <ColumnDefinition />
  165. </Grid.ColumnDefinitions>
  166. <ToggleButton x:Name="Expander"
  167. Style="{StaticResource ExpandCollapseToggleStyle}"
  168. IsChecked="{Binding Path=IsExpanded,
  169. RelativeSource={RelativeSource TemplatedParent}}"
  170. ClickMode="Press"/>
  171.  
  172. <ContentPresenter x:Name="PART_Header"
  173. Grid.Column="1"
  174. ContentSource="Header"
  175. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
  176. </Grid>
  177. </Border>
  178. <ItemsPresenter x:Name="ItemsHost" />
  179. </StackPanel>
  180. <ControlTemplate.Triggers>
  181. <Trigger Property="IsExpanded"
  182. Value="false">
  183. <Setter TargetName="ItemsHost"
  184. Property="Visibility"
  185. Value="Collapsed"/>
  186. </Trigger>
  187. <Trigger Property="HasItems"
  188. Value="false">
  189. <Setter TargetName="Expander"
  190. Property="Visibility"
  191. Value="Hidden"/>
  192. </Trigger>
  193. <MultiTrigger>
  194. <MultiTrigger.Conditions>
  195. <Condition Property="HasHeader"
  196. Value="false"/>
  197. <Condition Property="Width"
  198. Value="Auto"/>
  199. </MultiTrigger.Conditions>
  200. <Setter TargetName="PART_Header"
  201. Property="MinWidth"
  202. Value="75"/>
  203. </MultiTrigger>
  204. <MultiTrigger>
  205. <MultiTrigger.Conditions>
  206. <Condition Property="HasHeader"
  207. Value="false"/>
  208. <Condition Property="Height"
  209. Value="Auto"/>
  210. </MultiTrigger.Conditions>
  211. <Setter TargetName="PART_Header"
  212. Property="MinHeight"
  213. Value="19"/>
  214. </MultiTrigger>
  215. <Trigger Property="IsSelected"
  216. Value="true">
  217. <Setter TargetName="Bd"
  218. Property="Background"
  219. Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  220. <Setter Property="Foreground"
  221. Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  222. </Trigger>
  223. <MultiTrigger>
  224. <MultiTrigger.Conditions>
  225. <Condition Property="IsSelected"
  226. Value="true"/>
  227. <Condition Property="IsSelectionActive"
  228. Value="false"/>
  229. </MultiTrigger.Conditions>
  230. <Setter TargetName="Bd"
  231. Property="Background"
  232. Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  233. <Setter Property="Foreground"
  234. Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  235. </MultiTrigger>
  236. <Trigger Property="IsEnabled"
  237. Value="false">
  238. <Setter Property="Foreground"
  239. Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
  240. </Trigger>
  241. </ControlTemplate.Triggers>
  242. </ControlTemplate>
  243. </Setter.Value>
  244. </Setter>
  245. </Style>
  246.  
  247. <!-- ... -->
  248. <ControlTemplate TargetType="{x:Type TreeViewItem}">
  249. <Grid>
  250. <Grid.ColumnDefinitions>
  251. <ColumnDefinition MinWidth="19"
  252. Width="Auto"/>
  253. <ColumnDefinition Width="*"/>
  254. </Grid.ColumnDefinitions>
  255. <Grid.RowDefinitions>
  256. <RowDefinition Height="Auto"/>
  257. <RowDefinition/>
  258. </Grid.RowDefinitions>
  259. <!-- ... -->
  260.  
  261. <TreeView Margin="50" HorizontalContentAlignment="Stretch">
  262. <TreeViewItem Header="test2"/>
  263. <TreeViewItem Header="test2">
  264. <TreeViewItem Header="sub test"/>
  265. <TreeViewItem Header="sub test2"/>
  266. </TreeViewItem>
  267. <TreeViewItem Header="test3"/>
  268. </TreeView>
  269.  
  270. <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
  271.  
  272. <!--=================================================================
  273. TreeViewItem
  274. ==================================================================-->
  275. <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
  276. <Setter Property="Focusable" Value="False"/>
  277. <Setter Property="Template">
  278. <Setter.Value>
  279. <ControlTemplate TargetType="ToggleButton">
  280. <Grid
  281. Width="15"
  282. Height="13"
  283. Background="Transparent">
  284. <Path x:Name="ExpandPath"
  285. HorizontalAlignment="Left"
  286. VerticalAlignment="Center"
  287. Margin="1,1,1,1"
  288. Fill="{StaticResource GlyphBrush}"
  289. Data="M 4 0 L 8 4 L 4 8 Z"/>
  290. </Grid>
  291. <ControlTemplate.Triggers>
  292. <Trigger Property="IsChecked"
  293. Value="True">
  294. <Setter Property="Data"
  295. TargetName="ExpandPath"
  296. Value="M 0 4 L 8 4 L 4 8 Z"/>
  297. </Trigger>
  298. </ControlTemplate.Triggers>
  299. </ControlTemplate>
  300. </Setter.Value>
  301. </Setter>
  302. </Style>
  303. <Style x:Key="TreeViewItemFocusVisual">
  304. <Setter Property="Control.Template">
  305. <Setter.Value>
  306. <ControlTemplate>
  307. <Border>
  308. <Rectangle Margin="0,0,0,0"
  309. StrokeThickness="5"
  310. Stroke="Black"
  311. StrokeDashArray="1 2"
  312. Opacity="0"/>
  313. </Border>
  314. </ControlTemplate>
  315. </Setter.Value>
  316. </Setter>
  317. </Style>
  318.  
  319.  
  320. <Style x:Key="{x:Type TreeViewItem}"
  321. TargetType="{x:Type TreeViewItem}">
  322. <Setter Property="Background"
  323. Value="Transparent"/>
  324. <Setter Property="HorizontalContentAlignment"
  325. Value="{Binding Path=HorizontalContentAlignment,
  326. RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  327. <Setter Property="VerticalContentAlignment"
  328. Value="{Binding Path=VerticalContentAlignment,
  329. RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  330. <Setter Property="Padding"
  331. Value="1,0,0,0"/>
  332. <Setter Property="Foreground"
  333. Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  334. <Setter Property="FocusVisualStyle"
  335. Value="{StaticResource TreeViewItemFocusVisual}"/>
  336. <Setter Property="Template">
  337. <Setter.Value>
  338. <ControlTemplate TargetType="{x:Type TreeViewItem}">
  339. <Grid>
  340. <Grid.ColumnDefinitions>
  341. <ColumnDefinition MinWidth="19"
  342. Width="Auto"/>
  343. <ColumnDefinition Width="*"/>
  344. </Grid.ColumnDefinitions>
  345. <Grid.RowDefinitions>
  346. <RowDefinition Height="Auto"/>
  347. <RowDefinition/>
  348. </Grid.RowDefinitions>
  349. <ToggleButton x:Name="Expander"
  350. Style="{StaticResource ExpandCollapseToggleStyle}"
  351. IsChecked="{Binding Path=IsExpanded,
  352. RelativeSource={RelativeSource TemplatedParent}}"
  353. ClickMode="Press"/>
  354. <Border Name="Bd"
  355. Grid.Column="1"
  356. Background="{TemplateBinding Background}"
  357. BorderBrush="{TemplateBinding BorderBrush}"
  358. BorderThickness="{TemplateBinding BorderThickness}"
  359. Padding="{TemplateBinding Padding}">
  360. <ContentPresenter x:Name="PART_Header"
  361. ContentSource="Header"
  362. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
  363. </Border>
  364. <ItemsPresenter x:Name="ItemsHost"
  365. Grid.Row="1"
  366. Grid.Column="1"/>
  367. </Grid>
  368. <ControlTemplate.Triggers>
  369. <Trigger Property="IsExpanded" Value="false">
  370. <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
  371. </Trigger>
  372. <Trigger Property="HasItems" Value="false">
  373. <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
  374. </Trigger>
  375. <MultiTrigger>
  376. <MultiTrigger.Conditions>
  377. <Condition Property="HasHeader" Value="false"/>
  378. <Condition Property="Width" Value="Auto"/>
  379. </MultiTrigger.Conditions>
  380. <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
  381. </MultiTrigger>
  382. <MultiTrigger>
  383. <MultiTrigger.Conditions>
  384. <Condition Property="HasHeader" Value="false"/>
  385. <Condition Property="Height" Value="Auto"/>
  386. </MultiTrigger.Conditions>
  387. <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
  388. </MultiTrigger>
  389. <Trigger Property="IsSelected" Value="true">
  390. <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  391. <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  392. </Trigger>
  393. <MultiTrigger>
  394. <MultiTrigger.Conditions>
  395. <Condition Property="IsSelected" Value="true"/>
  396. <Condition Property="IsSelectionActive" Value="false"/>
  397. </MultiTrigger.Conditions>
  398. <Setter TargetName="Bd" Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  399. <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  400. </MultiTrigger>
  401. <Trigger Property="IsEnabled" Value="false">
  402. <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
  403. </Trigger>
  404. </ControlTemplate.Triggers>
  405. </ControlTemplate>
  406. </Setter.Value>
  407. </Setter>
  408. </Style>
  409.  
  410. <ControlTemplate TargetType="{x:Type TreeViewItem}">
  411. <StackPanel>
  412. <Border Name="Bd"
  413. Background="{TemplateBinding Background}"
  414. BorderBrush="{TemplateBinding BorderBrush}"
  415. BorderThickness="{TemplateBinding BorderThickness}"
  416. Padding="{TemplateBinding Padding}">
  417. <Grid>
  418. <Grid.ColumnDefinitions>
  419. <ColumnDefinition Width="19" />
  420. <ColumnDefinition />
  421. </Grid.ColumnDefinitions>
  422. <ToggleButton x:Name="Expander"
  423. Style="{StaticResource ExpandCollapseToggleStyle}"
  424. IsChecked="{Binding Path=IsExpanded,
  425. RelativeSource={RelativeSource TemplatedParent}}"
  426. ClickMode="Press"/>
  427. <ContentPresenter x:Name="PART_Header"
  428. Grid.Column="1"
  429. ContentSource="Header"
  430. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
  431. </Grid>
  432. </Border>
  433. <ItemsPresenter x:Name="ItemsHost" Margin="19,0,0,0" />
  434. </StackPanel>
  435. <!-- Triggers -->
  436. </ControlTemplate>
  437.  
  438. <TreeView Margin="50" HorizontalContentAlignment="Stretch">
  439. <TreeViewItem Header="test2"/>
  440. <TreeViewItem Header="test2">
  441. <TreeViewItem Header="sub test"/>
  442. <TreeViewItem Header="sub test2"/>
  443. </TreeViewItem>
  444. <TreeViewItem Header="test3"/>
  445. </TreeView>
  446.  
  447. <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
  448.  
  449. <!--=================================================================
  450. TreeViewItem
  451. ==================================================================-->
  452. <Style x:Key="ExpandCollapseToggleStyle" TargetType="ToggleButton">
  453. <Setter Property="Focusable" Value="False"/>
  454. <Setter Property="Template">
  455. <Setter.Value>
  456. <ControlTemplate TargetType="ToggleButton">
  457. <Grid
  458. Width="15"
  459. Height="13"
  460. Background="Transparent">
  461. <Path x:Name="ExpandPath"
  462. HorizontalAlignment="Left"
  463. VerticalAlignment="Center"
  464. Margin="1,1,1,1"
  465. Fill="{StaticResource GlyphBrush}"
  466. Data="M 4 0 L 8 4 L 4 8 Z"/>
  467. </Grid>
  468. <ControlTemplate.Triggers>
  469. <Trigger Property="IsChecked"
  470. Value="True">
  471. <Setter Property="Data"
  472. TargetName="ExpandPath"
  473. Value="M 0 4 L 8 4 L 4 8 Z"/>
  474. </Trigger>
  475. </ControlTemplate.Triggers>
  476. </ControlTemplate>
  477. </Setter.Value>
  478. </Setter>
  479. </Style>
  480. <Style x:Key="TreeViewItemFocusVisual">
  481. <Setter Property="Control.Template">
  482. <Setter.Value>
  483. <ControlTemplate>
  484. <Border>
  485. <Rectangle Margin="0,0,0,0"
  486. StrokeThickness="5"
  487. Stroke="Black"
  488. StrokeDashArray="1 2"
  489. Opacity="0"/>
  490. </Border>
  491. </ControlTemplate>
  492. </Setter.Value>
  493. </Setter>
  494. </Style>
  495.  
  496.  
  497. <Style x:Key="{x:Type TreeViewItem}"
  498. TargetType="{x:Type TreeViewItem}">
  499. <Setter Property="Background"
  500. Value="Transparent"/>
  501. <Setter Property="HorizontalContentAlignment"
  502. Value="{Binding Path=HorizontalContentAlignment,
  503. RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  504. <Setter Property="VerticalContentAlignment"
  505. Value="{Binding Path=VerticalContentAlignment,
  506. RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
  507. <Setter Property="Padding"
  508. Value="1,0,0,0"/>
  509. <Setter Property="Foreground"
  510. Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  511. <Setter Property="FocusVisualStyle"
  512. Value="{StaticResource TreeViewItemFocusVisual}"/>
  513. <Setter Property="Template">
  514. <Setter.Value>
  515. <ControlTemplate TargetType="{x:Type TreeViewItem}">
  516. <StackPanel>
  517. <Border Name="Bd"
  518. Background="{TemplateBinding Background}"
  519. BorderBrush="{TemplateBinding BorderBrush}"
  520. BorderThickness="{TemplateBinding BorderThickness}"
  521. Padding="{TemplateBinding Padding}">
  522. <Grid>
  523. <Grid.ColumnDefinitions>
  524. <ColumnDefinition Width="19" />
  525. <ColumnDefinition />
  526. </Grid.ColumnDefinitions>
  527. <ToggleButton x:Name="Expander"
  528. Style="{StaticResource ExpandCollapseToggleStyle}"
  529. IsChecked="{Binding Path=IsExpanded,
  530. RelativeSource={RelativeSource TemplatedParent}}"
  531. ClickMode="Press"/>
  532. <ContentPresenter x:Name="PART_Header"
  533. Grid.Column="1"
  534. ContentSource="Header"
  535. HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
  536. </Grid>
  537. </Border>
  538. <ItemsPresenter x:Name="ItemsHost" Margin="19,0,0,0" />
  539. </StackPanel>
  540. <ControlTemplate.Triggers>
  541. <Trigger Property="IsExpanded"
  542. Value="false">
  543. <Setter TargetName="ItemsHost"
  544. Property="Visibility"
  545. Value="Collapsed"/>
  546. </Trigger>
  547. <Trigger Property="HasItems"
  548. Value="false">
  549. <Setter TargetName="Expander"
  550. Property="Visibility"
  551. Value="Hidden"/>
  552. </Trigger>
  553. <MultiTrigger>
  554. <MultiTrigger.Conditions>
  555. <Condition Property="HasHeader"
  556. Value="false"/>
  557. <Condition Property="Width"
  558. Value="Auto"/>
  559. </MultiTrigger.Conditions>
  560. <Setter TargetName="PART_Header"
  561. Property="MinWidth"
  562. Value="75"/>
  563. </MultiTrigger>
  564. <MultiTrigger>
  565. <MultiTrigger.Conditions>
  566. <Condition Property="HasHeader"
  567. Value="false"/>
  568. <Condition Property="Height"
  569. Value="Auto"/>
  570. </MultiTrigger.Conditions>
  571. <Setter TargetName="PART_Header"
  572. Property="MinHeight"
  573. Value="19"/>
  574. </MultiTrigger>
  575. <Trigger Property="IsSelected"
  576. Value="true">
  577. <Setter TargetName="Bd"
  578. Property="Background"
  579. Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
  580. <Setter Property="Foreground"
  581. Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
  582. </Trigger>
  583. <MultiTrigger>
  584. <MultiTrigger.Conditions>
  585. <Condition Property="IsSelected"
  586. Value="true"/>
  587. <Condition Property="IsSelectionActive"
  588. Value="false"/>
  589. </MultiTrigger.Conditions>
  590. <Setter TargetName="Bd"
  591. Property="Background"
  592. Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
  593. <Setter Property="Foreground"
  594. Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
  595. </MultiTrigger>
  596. <Trigger Property="IsEnabled"
  597. Value="false">
  598. <Setter Property="Foreground"
  599. Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
  600. </Trigger>
  601. </ControlTemplate.Triggers>
  602. </ControlTemplate>
  603. </Setter.Value>
  604. </Setter>
  605. </Style>
  606.  
  607. public static class TreeViewItemExtensions
  608. {
  609. public static int GetDepth(this TreeViewItem item)
  610. {
  611. while (GetSelectedTreeViewItemParent(item) != null)
  612. {
  613. var parent = GetSelectedTreeViewItemParent(item);
  614. if (parent != null)
  615. return parent.GetDepth() + 1;
  616.  
  617. item = parent;
  618. }
  619. return 0;
  620. }
  621.  
  622. public static TreeViewItem GetSelectedTreeViewItemParent(this TreeViewItem item)
  623. {
  624. DependencyObject parent = VisualTreeHelper.GetParent(item);
  625. while (!(parent is TreeViewItem || parent is TreeView))
  626. {
  627. parent = VisualTreeHelper.GetParent(parent);
  628. }
  629.  
  630. return parent as TreeViewItem;
  631. }
  632. }
  633.  
  634. <ControlTemplate TargetType="{x:Type TreeViewItem}">
  635. <Grid>
  636. <Grid.ColumnDefinitions>
  637. <ColumnDefinition MinWidth="19" Width="Auto"/>
  638. <ColumnDefinition Width="*"/>
  639. </Grid.ColumnDefinitions>
  640. <Grid.RowDefinitions>
  641. <RowDefinition Height="Auto"/>
  642. <RowDefinition/>
  643. </Grid.RowDefinitions>
  644. <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" VerticalAlignment="Top" Panel.ZIndex="1"/>
  645. <Rectangle x:Name="Hb" Width="Auto" Height="Auto" Grid.ColumnSpan="2" Margin="-100,0,0,0" Panel.ZIndex="-1" Visibility="Hidden" />
  646. <Border x:Name="Bd" SnapsToDevicePixels="true" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" Grid.Column="1" Panel.ZIndex="0">
  647. <ContentPresenter x:Name="PART_Header" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" HorizontalAlignment="Stretch"/>
  648. </Border>
  649. <ItemsPresenter x:Name="ItemsHost" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Margin="19,0,0,0"/>
  650. </Grid>
  651.  
  652. public static int GetDepth(this TreeViewItem item)
  653. {
  654. FrameworkElement elem = item;
  655. var parent = VisualTreeHelper.GetParent(item);
  656. var count = 0;
  657. while (parent != null && !(parent is TreeView))
  658. {
  659. var tvi = parent as TreeViewItem;
  660. if (parent is TreeViewItem)
  661. count++;
  662. parent = VisualTreeHelper.GetParent(parent);
  663. }
  664. return count;
  665. }
Add Comment
Please, Sign In to add comment