XAML PowerShell WPF TreeView与动态图像

evrscar2  于 2023-11-14  发布在  Shell
关注(0)|答案(1)|浏览(315)

我正在做一个项目,我有一个PowerShell驱动的UI,有一个选择的Treeview列表。我有这个代码工作得很好,下面这个博客PowerShell WPF - Customize TreeView Icon.伟大的文章顺便说一句!我想自定义树列表与不同的图像为不同的文件的基础上,其项目标签。我可以得到这个工作使用PNG图像源和 DataTriggers,然而,我正试图将所有图像作为SVG嵌入到画布模板中。我知道如何使其工作的唯一方法是使用 VisualBrush 元素,但我似乎无法在Treeview.resource framework元素中使用它。以下是我迄今为止所做的:

  1. <Window
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="TreeView"
  5. Height="600" Width="800"
  6. ResizeMode="NoResize"
  7. WindowStartupLocation="CenterScreen"
  8. ShowInTaskbar="False" Topmost="True">
  9. <Window.Resources>
  10. <ResourceDictionary>
  11. <ResourceDictionary.MergedDictionaries>
  12. <ResourceDictionary Source="Resources\Icons.xaml" />
  13. <ResourceDictionary Source="Resources\TabControl_LeftSideStyle.xaml" />
  14. <ResourceDictionary Source="Resources\TreeViewItem_StandardStyle.xaml" />
  15. </ResourceDictionary.MergedDictionaries>
  16. <Style TargetType="{x:Type Window}">
  17. <Setter Property="FontFamily" Value="Segoe UI" />
  18. <Setter Property="FontWeight" Value="Light" />
  19. <Setter Property="BorderBrush" Value="#004275" />
  20. <Setter Property="BorderThickness" Value="0.5" />
  21. </Style>
  22. <HierarchicalDataTemplate x:Key="CheckBoxItemTemplate" ItemsSource="{Binding Children, Mode=OneTime}">
  23. <StackPanel Orientation="Horizontal">
  24. <CheckBox Focusable="False" IsChecked="{Binding IsChecked}" VerticalAlignment="Center" />
  25. <ContentPresenter Content="{Binding Name, Mode=OneTime}" Margin="2,0" />
  26. </StackPanel>
  27. </HierarchicalDataTemplate>
  28. </ResourceDictionary>
  29. </Window.Resources>
  30. <Grid>
  31. <Rectangle Fill="#004275" HorizontalAlignment="Left" Height="600" VerticalAlignment="Center" Width="150"/>
  32. <Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Right" Height="600" VerticalAlignment="Center" Width="156"/>
  33. <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10">
  34. <Image x:Name="_wizMainLogo" Height="48" Width="132" Stretch="Uniform" />
  35. </StackPanel>
  36. <!-- START TAB MENU -->
  37. <TabControl x:Name="_wizTabControl" Style="{DynamicResource TabControlLeftSide}" Width="800" Height="550" HorizontalAlignment="Center" VerticalAlignment="Top">
  38. <TabItem x:Name="_wizid" Header="Tree view" Style="{DynamicResource TabItemsWhite}" Width="150" Margin="0" IsEnabled="False">
  39. <Grid x:Name="TabLayout" Margin="0" Grid.ColumnSpan="2" FocusManager.FocusedElement="{Binding ElementName=tsTree}">
  40. <Grid.ColumnDefinitions>
  41. <ColumnDefinition Width="490"></ColumnDefinition>
  42. <ColumnDefinition Width="150"></ColumnDefinition>
  43. </Grid.ColumnDefinitions>
  44. <Label x:Name="TabMainTitle" Grid.Column="0" HorizontalAlignment="Left" Margin="10,20,0,0" VerticalAlignment="Top" FontSize="22" Content="Tree view list with icons"/>
  45. <Rectangle Grid.Column="0" Fill="HotPink" HorizontalAlignment="Right" Height="40" Width="40" Margin="0,20,13.5,0" VerticalAlignment="Top">
  46. <Rectangle.OpacityMask>
  47. <VisualBrush Stretch="Fill" Visual="{DynamicResource icons_listoutline}"/>
  48. </Rectangle.OpacityMask>
  49. </Rectangle>
  50. <Label x:Name="TabSubTitle" FontSize="14" HorizontalAlignment="Left" Margin="10,73,0,0" VerticalAlignment="Top" Content="Search for item in list"/>
  51. <TextBox x:Name="TabSearch" HorizontalAlignment="Left" Height="31" Margin="10,103,0,0" TextWrapping="Wrap" Text="Search..." VerticalAlignment="Top" Width="331" Foreground='Gray' VerticalContentAlignment="Center" FontSize="18"/>
  52. <Button x:Name="TabSearchClear" Content="Clear" Height="31" Width="63" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="10" Padding="2" Margin="414,0,0,416" />
  53. <Button x:Name="TabSearchEnter" Content="Search" Height="31" Width="63" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="10" Padding="2" Margin="346,0,0,416" />
  54. <Button x:Name="TabExpand" Content="Expand All" Height="25" Width="113" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="10" Padding="2" Margin="10,0,0,386" />
  55. <Button x:Name="TabCollapse" Content="Collapse All" Height="25" Width="113" HorizontalAlignment="Left" VerticalAlignment="Bottom" FontSize="10" Padding="2" Margin="128,0,0,386" />
  56. <TreeView x:Name="TabTree" HorizontalAlignment="Left" Height="371" Margin="10,169,0,0" VerticalAlignment="Top" Width="467" FontSize="14"
  57. ItemContainerStyle="{StaticResource TreeViewItemStandard}"
  58. ItemTemplate="{StaticResource CheckBoxItemTemplate}" >
  59. <TreeView.Resources>
  60. <Style TargetType="{x:Type TreeViewItem}">
  61. <Setter Property="HeaderTemplate">
  62. <Setter.Value>
  63. <DataTemplate>
  64. <StackPanel Orientation="Horizontal">
  65. <Image Name="TreeNodeIMG" Width="20" Height="20" Stretch="Fill">
  66. <Image.Style>
  67. <Style TargetType="{x:Type Image}">
  68. <Style.Triggers>
  69. <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Tag[0]}" Value="folder">
  70. <Setter Property="Source" Value="images\folder.png"/>
  71. </DataTrigger>
  72. <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Tag[0]}" Value="file">
  73. <Setter Property="Source" Value="images\file.png"/>
  74. </DataTrigger>
  75. </Style.Triggers>
  76. </Style>
  77. </Image.Style>
  78. </Image>
  79. <TextBlock VerticalAlignment="Center" Text="{Binding}" Margin="5,0" />
  80. </StackPanel>
  81. </DataTemplate>
  82. </Setter.Value>
  83. </Setter>
  84. </Style>
  85. </TreeView.Resources>
  86. </TreeView>
  87. <Label Content="More Info" Grid.Column="1" FontSize="14" HorizontalAlignment="Left" Margin="10,31,0,0" VerticalAlignment="Top" Foreground="LightSlateGray" />
  88. <TextBlock x:Name="TabMoreInfo" Grid.Column="1" HorizontalAlignment="Left" Margin="10,89,0,0" Width="136" TextWrapping="Wrap" VerticalAlignment="Top" Height="422">
  89. <Run Text="orem ipsum dolor sit amet, consectetur adipiscing elit. Morbi risus mi, consequat in ultricies eu, euismod nec eros. Pellentesque vel augue sed enim euismod sodales varius a ipsum. "/>
  90. </TextBlock>
  91. </Grid>
  92. </TabItem>
  93. </TabControl>
  94. </Grid>
  95. </Window>

字符串
我正在测试的PowerShell脚本示例是这样的:

  1. #$ErrorActionPreference='Stop'
  2. ##*=============================================
  3. ##* VARIABLES
  4. ##*=============================================
  5. [string]$ResourceRoot = ($PWD.ProviderPath, $PSScriptRoot)[[bool]$PSScriptRoot]
  6. ##*=============================================
  7. ##* LOAD UI
  8. ##*=============================================
  9. [System.Reflection.Assembly]::LoadWithPartialName('presentationframework') | out-null
  10. function LoadXaml ($filename){
  11. $XamlLoader=(New-Object System.Xml.XmlDocument)
  12. $XamlLoader.Load($filename)
  13. return $XamlLoader
  14. }
  15. $XamlMainWindow=LoadXaml($ResourceRoot+"\MainWindow.xaml")
  16. $reader = (New-Object System.Xml.XmlNodeReader $XamlMainWindow)
  17. $Form = [Windows.Markup.XamlReader]::Load($reader)
  18. $Global:FolderTree = $Form.FindName("TabTree")
  19. ##*=============================================
  20. ##* MAIN
  21. ##*=============================================
  22. $dummyNode = $null
  23. $preselect = 'rootFile1.txt'
  24. $AllFiles = [IO.Directory]::GetFiles("$ResourceRoot\Test")
  25. $AllDirectory = [IO.Directory]::GetDirectories("$ResourceRoot\Test")
  26. # ================== Handle Folders ===========================
  27. #$folder = $AllDirectory[0]
  28. foreach ($folder in $AllDirectory){
  29. $treeViewItem = [Windows.Controls.TreeViewItem]::new()
  30. $treeViewItem.Header = $folder.Substring($folder.LastIndexOf("\") + 1)
  31. $treeViewItem.Tag = @("folder",$folder)
  32. $treeViewItem.Items.Add($dummyNode) | Out-Null
  33. $treeViewItem.Add_Expanded({
  34. Write-Host $_.OriginalSource.Header " is expanded"
  35. TreeExpanded($_.OriginalSource)
  36. })
  37. $FolderTree.Items.Add($treeViewItem)| Out-Null
  38. }
  39. # ================== Handle Files ===========================
  40. foreach ($file in $AllFiles){
  41. $treeViewItem = [Windows.Controls.TreeViewItem]::new()
  42. $treeViewItem.Header = $file.Substring($file.LastIndexOf("\") + 1)
  43. $treeViewItem.Tag = @("file",$file)
  44. $FolderTree.Items.Add($treeViewItem)| Out-Null
  45. If($treeViewItem.Header -eq $preselect){
  46. $treeViewItem.IsSelected = $true
  47. Write-Host ("pre selected Item: {0}" -f $treeViewItem.Header)
  48. }Else{
  49. Write-Host ("Added Item: {0}" -f $treeViewItem.Header)
  50. }
  51. $treeViewItem.Add_PreviewMouseLeftButtonDown({
  52. [System.Windows.Controls.TreeViewItem]$sender = $args[0]
  53. [System.Windows.RoutedEventArgs]$e = $args[1]
  54. Write-Host "Left Click: $($sender.Tag)"
  55. })
  56. $treeViewItem.Add_PreviewMouseRightButtonDown({
  57. [System.Windows.Controls.TreeViewItem]$sender = $args[0]
  58. [System.Windows.RoutedEventArgs]$e = $args[1]
  59. Write-Host "Right Click: $($sender.Tag)"
  60. })
  61. }
  62. Function TreeExpanded($sender){
  63. $global:item = [Windows.Controls.TreeViewItem]$sender
  64. If ($item.Items.Count -eq 1 -and $item.Items[0] -eq $dummyNode)
  65. {
  66. $item.Items.Clear();
  67. Try
  68. {
  69. foreach ($string in [IO.Directory]::GetDirectories($item.Tag[1].ToString()))
  70. {
  71. $subitem = [Windows.Controls.TreeViewItem]::new();
  72. $subitem.Header = $string.Substring($string.LastIndexOf("\") + 1)
  73. $subitem.Tag = @("folder",$string)
  74. $subitem.Items.Add($dummyNode)
  75. $subitem.Add_Expanded({
  76. TreeExpanded($_.OriginalSource)
  77. })
  78. $item.Items.Add($subitem) | Out-Null
  79. }
  80. foreach ($file in [IO.Directory]::GetFiles($item.Tag[1].ToString())){
  81. $subitem = [Windows.Controls.TreeViewItem]::new()
  82. $subitem.Header = $file.Substring($file.LastIndexOf("\") + 1)
  83. $subitem.Tag = @("file",$file)
  84. $item.Items.Add($subitem)| Out-Null
  85. If($subitem.Header -eq $preselect){
  86. $subitem.IsSelected = $true
  87. Write-Host ("pre-selected Item: {0}" -f $subitem.Header)
  88. }Else{
  89. Write-Host ("Added Item: {0}" -f $subitem.Header)
  90. }
  91. $subitem.Add_PreviewMouseLeftButtonDown({
  92. [System.Windows.Controls.TreeViewItem]$sender = $args[0]
  93. [System.Windows.RoutedEventArgs]$e = $args[1]
  94. Write-Host "Left Click: $($sender.Tag)"
  95. })
  96. }
  97. }
  98. Catch [Exception] { }
  99. }
  100. }
  101. ##*=============================================
  102. ##* SHOW UI
  103. ##*=============================================
  104. $Form.ShowDialog() | Out-Null


当我运行代码时,它看起来像这样:
x1c 0d1x的数据
这是一个问题的图像不显示为根级别的文件夹,但不是太大的交易,除非有人有一个答案太.
canvas资源字典文件中包含以下内容:

  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  3. <Canvas x:Key="icon_file" Width="24" Height="24">
  4. <Path Stretch="Fill" Fill="White" Data="M3 5V19H20V5H3M7 7V9H5V7H7M5 13V11H7V13H5M5 15H7V17H5V15M18 17H9V15H18V17M18 13H9V11H18V13M18 9H9V7H18V9Z" />
  5. </Canvas>
  6. <Canvas x:Key="icon_folder" Width="24" Height="24">
  7. <Path Width="20" Height="20" Fill="White" Stretch="Uniform" Data="M19,20H4C2.89,20 2,19.1 2,18V6C2,4.89 2.89,4 4,4H10L12,6H19A2,2 0 0,1 21,8H21L4,8V18L6.14,10H23.21L20.93,18.5C20.7,19.37 19.92,20 19,20Z"/>
  8. </Canvas>
  9. </ResourceDictionary>


我让它工作的唯一方法是在XAML中使用这段代码

  1. <TreeView.Resources>
  2. <Style TargetType="{x:Type TreeViewItem}">
  3. <Setter Property="HeaderTemplate">
  4. <Setter.Value>
  5. <DataTemplate>
  6. <StackPanel Orientation="Horizontal">
  7. <Rectangle Fill="Green" HorizontalAlignment="Right" Height="20" Width="20" VerticalAlignment="Top">
  8. <Rectangle.OpacityMask>
  9. <VisualBrush Stretch="Fill" Visual="{DynamicResource icons_folder}"/>
  10. </Rectangle.OpacityMask>
  11. </Rectangle>
  12. <TextBlock Text="{Binding}" Margin="5,0" />
  13. </StackPanel>
  14. </DataTemplate>
  15. </Setter.Value>
  16. </Setter>
  17. </Style>
  18. </TreeView.Resources>


即使这填充整个图像的填充颜色,它确实显示,但其相同的文件夹和文件。现在,如果我试图使用画布图像和数据触发器的绑定,我得到一个数据类型错误。我在网上没有找到任何关于如何使用svg数据绑定与treeview和数据触发器的内容。有很多C#编码,但正在寻找PowerShell和.Net解决方案。
尽管我认为在XAML代码中实现这一点会更容易,但我确实尝试过在PowerShell中以编程方式更改图像......但这也失败了。
我需要你的一些专业技能!谢谢!

vi4fp9gy

vi4fp9gy1#

您可以将HeaderTemplate中的Image替换为显示您的Canvas资源之一的ContentControl

  1. <Style TargetType="{x:Type TreeViewItem}">
  2. <Setter Property="HeaderTemplate">
  3. <Setter.Value>
  4. <DataTemplate>
  5. <StackPanel Orientation="Horizontal">
  6. <ContentControl>
  7. <ContentControl.Style>
  8. <Style TargetType="ContentControl">
  9. <Style.Triggers>
  10. <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Tag[0]}" Value="folder">
  11. <Setter Property="Content" Value="{DynamicResource icon_folder}" />
  12. </DataTrigger>
  13. <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=Tag[0]}" Value="file">
  14. <Setter Property="Content" Value="{DynamicResource icon_file}" />
  15. </DataTrigger>
  16. </Style.Triggers>
  17. </Style>
  18. </ContentControl.Style>
  19. </ContentControl>
  20. <TextBlock VerticalAlignment="Center" Text="{Binding}" Margin="5,0" />
  21. </StackPanel>
  22. </DataTemplate>
  23. </Setter.Value>
  24. </Setter>
  25. </Style>

字符串

展开查看全部

相关问题