WPF TreeView

  • WPF

Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von GOKTerek.

    WPF TreeView

    Hallo Liebe Gemeinde,

    ich habe ein kleines Problem mit meinem Treeview control. undzwar möchte ich das Image bei einem Expandierten Node ändern. Leider funktioniort das nicht. Bzw. schon nach einem Refresh, nur sind dann alle Nodes collapsed.

    Anbei mein XAML Code.

    Spoiler anzeigen

    XML-Quellcode

    1. <Page
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    6. xmlns:local="clr-namespace:SWPDM_EnterPrise_Functions_001"
    7. xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="Folder_Selection"
    8. mc:Ignorable="d"
    9. d:DesignHeight="450" d:DesignWidth="800">
    10. <Page.Resources>
    11. <ResourceDictionary>
    12. <ResourceDictionary.MergedDictionaries>
    13. <ResourceDictionary Source="GOK_Style_Ressource.xaml"/>
    14. </ResourceDictionary.MergedDictionaries>
    15. </ResourceDictionary>
    16. </Page.Resources>
    17. <Grid>
    18. <DockPanel Background="White">
    19. <DockPanel DockPanel.Dock="Top" Height="42" VerticalAlignment="Top" >
    20. <Border x:Name="Border_01" DockPanel.Dock="Bottom" Background="#FF0162A6" Height="2" Margin="0" VerticalAlignment="Bottom"/>
    21. <TextBox x:Name="TXT_User" Text="User" Width="120" Margin="5,5,0,5" HorizontalAlignment="Left" Style="{StaticResource FontStyle_002}"/>
    22. <TextBox x:Name="TXT_PW" Text="User PW" Width="150" Margin="5,5,0,5" HorizontalAlignment="Left" Style="{StaticResource FontStyle_002}"/>
    23. <Button x:Name="BTN_Refresh_Tabel" Height="30" Width="30" HorizontalAlignment="Left" Margin="5,5,5,5" Style="{StaticResource Button_001}">
    24. <Image x:Name="BTN_Refresh_Tabel_Image" Stretch="Uniform"/>
    25. </Button>
    26. </DockPanel>
    27. <DockPanel DockPanel.Dock="Bottom" Background="#FFFDFDFD">
    28. <DockPanel DockPanel.Dock="Left" Width="Auto" MinWidth="250">
    29. <TreeView x:Name="FolderTreeView" TreeViewItem.Expanded="TreeViewItem_Expanded">
    30. <TreeView.ItemTemplate>
    31. <HierarchicalDataTemplate DataType="{x:Type local:FolderTree}" ItemsSource="{Binding SubItems}">
    32. <StackPanel Orientation="Horizontal" Height="22">
    33. <CheckBox Focusable="False" IsChecked="{Binding Checked}" VerticalAlignment="Center"/>
    34. <Image Name="FolderImage" Width="18" Height="18" Margin="5,0,0,0" Source="{Binding Image}" Stretch="Uniform"/>
    35. <TextBlock Text="{Binding Folder}" Style="{StaticResource FontStyle_004}" Margin="5,0,10,0" VerticalAlignment="Center"/>
    36. </StackPanel>
    37. <HierarchicalDataTemplate.Triggers>
    38. <DataTrigger Binding="{Binding IsExpanded_hnd}" Value="True">
    39. <Setter Property="Image.Source" Value="{Binding OpenImage}" TargetName="FolderImage"/>
    40. </DataTrigger>
    41. </HierarchicalDataTemplate.Triggers>
    42. </HierarchicalDataTemplate>
    43. </TreeView.ItemTemplate>
    44. </TreeView>
    45. </DockPanel>
    46. <DockPanel DockPanel.Dock="Right">
    47. <Grid>
    48. <DataGrid x:Name="DGR_001" Style="{StaticResource DataGridStyle_001}" Grid.Column="0" VirtualizingStackPanel.IsVirtualizing="False" Margin="0,0,1,0" ToolTip="Liste aller Sitzungen" CanUserAddRows="False" CanUserReorderColumns="False"/>
    49. </Grid>
    50. </DockPanel>
    51. </DockPanel>
    52. </DockPanel>
    53. </Grid>
    54. </Page>


    und hier mein VB.Net Code.

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports System.Windows.Forms
    3. Imports System.Drawing
    4. Imports EdmLib
    5. Imports System.Windows
    6. Imports System.Windows.Input
    7. Public Class Folder_Selection
    8. Dim Folder_hnd As String = ""
    9. Dim VaultConn As IEdmVault5
    10. Dim SWPDM_MainInstance As Main_Instance = New Main_Instance
    11. Private GOKMF_Main_Instance As New GOK_Main_Functions.Main_Instance
    12. Private GOKMF_Image_Functions As New GOK_Main_Functions.GOK_Image_Functions(GOKMF_Main_Instance)
    13. Dim Get_Files As Boolean = False
    14. Dim FullFolderTree As FolderTree
    15. Public Sub New(ByVal PDM_Server As String, Optional ByVal GetFiles As Boolean = False)
    16. InitializeComponent()
    17. Select Case PDM_Server
    18. Case "GOK_SW_PDM"
    19. Folder_hnd = "C:\SW_PDM\"
    20. Case "GOK_TEST_PDM"
    21. Folder_hnd = "C:\TEST_PDM\"
    22. End Select
    23. SWPDM_MainInstance.GetNewVaultConnection(PDM_Server)
    24. Me.TXT_User.Text = SWPDM_MainInstance.GetCurrentUserName
    25. Get_Files = GetFiles
    26. FullFolderTree = GetFolderTree(Folder_hnd)
    27. Me.FolderTreeView.Items.Add(FullFolderTree)
    28. Me.BTN_Refresh_Tabel_Image.Source = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Reload"), Image))
    29. End Sub
    30. Private Sub FolderTreeView_SelectedItemChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Object)) Handles FolderTreeView.SelectedItemChanged
    31. Dim TempFolderTree As FolderTree = DirectCast(Me.FolderTreeView.SelectedItem, FolderTree)
    32. End Sub
    33. Public Function GetFolderTree(ByVal Main_Folder As String, Optional ByVal PDMImage As Boolean = True) As FolderTree
    34. Dim Returning_FolderTree As FolderTree = New FolderTree
    35. Dim Temp_DirectoryInfo As IO.DirectoryInfo = New IO.DirectoryInfo(Main_Folder)
    36. If PDMImage Then
    37. Returning_FolderTree.Folder = Main_Folder
    38. Returning_FolderTree.FullName = Temp_DirectoryInfo.FullName
    39. Returning_FolderTree.Checked = False
    40. Returning_FolderTree.Image = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("SW_PDM_Icon"), Image))
    41. Returning_FolderTree.OpenImage = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("SW_PDM_Icon"), Image))
    42. Else
    43. Returning_FolderTree.Folder = Temp_DirectoryInfo.Name
    44. Returning_FolderTree.FullName = Temp_DirectoryInfo.FullName
    45. Returning_FolderTree.Checked = False
    46. Returning_FolderTree.Image = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Folder_Closed"), Image))
    47. Returning_FolderTree.OpenImage = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Folder_Open"), Image))
    48. End If
    49. If Get_Files Then
    50. End If
    51. For Each SubFolder As IO.DirectoryInfo In Temp_DirectoryInfo.GetDirectories
    52. Returning_FolderTree.SubItems.Add(GetFolderTree(SubFolder.FullName, False))
    53. Next
    54. Return Returning_FolderTree
    55. End Function
    56. Public Sub TreeViewItem_Expanded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    57. 'TODO: Ändern Bild wenn Treenode is expanded
    58. Dim Item As System.Windows.Controls.TreeViewItem = CType(e.OriginalSource, System.Windows.Controls.TreeViewItem)
    59. If Item.IsExpanded Then
    60. Dim temp As FolderTree = CType(Item.DataContext, FolderTree)
    61. temp.IsExpanded_hnd = Item.IsExpanded
    62. End If
    63. End Sub
    64. End Class
    65. Public Class FolderTree
    66. Public Property Image As System.Windows.Media.Imaging.BitmapImage
    67. Public Property OpenImage As System.Windows.Media.Imaging.BitmapImage
    68. Public Property Folder As String
    69. Public Property FullName As String
    70. Public Property IsExpanded_hnd As Boolean = False
    71. Public Property Checked As Boolean
    72. Public ReadOnly Property SubItems As List(Of FolderTree) = New List(Of FolderTree)()
    73. End Class



    Was mache ich falsch?

    Gruß
    Hallo Liebe Gemeinde,

    habe nun eine Lösung gefunden, bin mir aber nicht sicher ob das so richtig ist. Es funktioniert zumindest.

    Anbei die XAML.

    Spoiler anzeigen

    XML-Quellcode

    1. <Page
    2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    6. xmlns:local="clr-namespace:SWPDM_EnterPrise_Functions_001"
    7. xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="Folder_Selection"
    8. mc:Ignorable="d"
    9. d:DesignHeight="450" d:DesignWidth="800">
    10. <Page.Resources>
    11. <ResourceDictionary>
    12. <ResourceDictionary.MergedDictionaries>
    13. <ResourceDictionary Source="GOK_Style_Ressource.xaml"/>
    14. </ResourceDictionary.MergedDictionaries>
    15. </ResourceDictionary>
    16. </Page.Resources>
    17. <Grid>
    18. <DockPanel Background="White">
    19. <DockPanel DockPanel.Dock="Top" Height="42" VerticalAlignment="Top" >
    20. <Border x:Name="Border_01" DockPanel.Dock="Bottom" Background="#FF0162A6" Height="2" Margin="0" VerticalAlignment="Bottom"/>
    21. <TextBox x:Name="TXT_User" Text="User" Width="120" Margin="5,5,0,5" HorizontalAlignment="Left" Style="{StaticResource FontStyle_002}"/>
    22. <TextBox x:Name="TXT_PW" Text="User PW" Width="150" Margin="5,5,0,5" HorizontalAlignment="Left" Style="{StaticResource FontStyle_002}"/>
    23. <Button x:Name="BTN_Refresh_Tabel" Height="30" Width="30" HorizontalAlignment="Left" Margin="5,5,5,5" Style="{StaticResource Button_001}">
    24. <Image x:Name="BTN_Refresh_Tabel_Image" Stretch="Uniform"/>
    25. </Button>
    26. <Button x:Name="BTN_Select_All" Height="30" Width="30" HorizontalAlignment="Left" Margin="5,5,5,5" Style="{StaticResource Button_001}">
    27. <Image x:Name="BTN_Select_All_Image" Stretch="Uniform"/>
    28. </Button>
    29. <Button x:Name="BTN_DeSelect_All" Height="30" Width="30" HorizontalAlignment="Left" Margin="5,5,5,5" Style="{StaticResource Button_001}">
    30. <Image x:Name="BTN_DeSelect_All_Image" Stretch="Uniform"/>
    31. </Button>
    32. </DockPanel>
    33. <DockPanel DockPanel.Dock="Bottom" Background="#FFFDFDFD">
    34. <DockPanel DockPanel.Dock="Left" Width="Auto" MinWidth="250">
    35. <TreeView x:Name="FolderTreeView" TreeViewItem.Expanded="TreeViewItem_Expanded" TreeViewItem.Collapsed="TreeViewItem_Expanded" VirtualizingStackPanel.IsVirtualizing="False">
    36. <TreeView.ItemTemplate>
    37. <HierarchicalDataTemplate DataType="{x:Type local:FolderTree}" ItemsSource="{Binding SubItems}">
    38. <StackPanel Orientation="Horizontal" Height="22">
    39. <CheckBox Name="CHKBox" Focusable="False" IsChecked="{Binding Checked}" VerticalAlignment="Center"/>
    40. <Image Name="FolderImage" Width="18" Height="18" Margin="5,0,0,0" Source="{Binding Image}" Stretch="Uniform"/>
    41. <TextBlock Text="{Binding Folder}" Style="{StaticResource FontStyle_004}" Margin="5,0,10,0" VerticalAlignment="Center"/>
    42. </StackPanel>
    43. </HierarchicalDataTemplate>
    44. </TreeView.ItemTemplate>
    45. <TreeView.ItemContainerStyle>
    46. <Style TargetType="{x:Type TreeViewItem}">
    47. <Setter Property="IsExpanded" Value="{Binding IsExpanded_hnd, Mode=TwoWay}" />
    48. </Style>
    49. </TreeView.ItemContainerStyle>
    50. </TreeView>
    51. </DockPanel>
    52. <DockPanel DockPanel.Dock="Right">
    53. <Grid>
    54. <DataGrid x:Name="DGR_001" Style="{StaticResource DataGridStyle_001}" Grid.Column="0" VirtualizingStackPanel.IsVirtualizing="False" Margin="0,0,1,0" ToolTip="Liste aller Sitzungen" CanUserAddRows="False" CanUserReorderColumns="False"/>
    55. </Grid>
    56. </DockPanel>
    57. </DockPanel>
    58. </DockPanel>
    59. </Grid>
    60. </Page>


    und hier der VB.Net code dazu:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports System.Windows.Forms
    3. Imports System.Drawing
    4. Imports EdmLib
    5. Imports System.Windows
    6. Imports System.Windows.Input
    7. Imports System.Windows.Controls
    8. Public Class Folder_Selection
    9. Dim Folder_hnd As String = ""
    10. Dim Datasource As String
    11. Dim VaultConn As IEdmVault5
    12. Dim SWPDM_MainInstance As Main_Instance = New Main_Instance
    13. Dim TreeViewUpdate As Boolean = False
    14. Private GOKMF_Main_Instance As New GOK_Main_Functions.Main_Instance
    15. Private GOKMF_Image_Functions As New GOK_Main_Functions.GOK_Image_Functions(GOKMF_Main_Instance)
    16. Dim Get_Files As Boolean = False
    17. Dim FullFolderTree As FolderTree
    18. Public ServerFolderTable As New DataTable
    19. Public Sub New(ByVal PDM_Server As String, Optional ByVal GetFiles As Boolean = False)
    20. InitializeComponent()
    21. Datasource = PDM_Server
    22. Select Case PDM_Server
    23. Case "GOK_SW_PDM"
    24. Folder_hnd = "C:\GOK_SW_PDM\"
    25. Case "GOK_TEST_PDM"
    26. Folder_hnd = "C:\GOK_TEST_PDM\"
    27. End Select
    28. SWPDM_MainInstance.GetNewVaultConnection(PDM_Server)
    29. Me.TXT_User.Text = SWPDM_MainInstance.GetCurrentUserName
    30. Get_Files = GetFiles
    31. FullFolderTree = GetFolderTree(Folder_hnd)
    32. Me.FolderTreeView.Items.Add(FullFolderTree)
    33. Me.BTN_Refresh_Tabel_Image.Source = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Reload"), System.Drawing.Image))
    34. Me.BTN_Select_All_Image.Source = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Select_All"), System.Drawing.Image))
    35. For Each NewColumn As DataColumn In GetColumns()
    36. ServerFolderTable.Columns.Add(NewColumn)
    37. Next
    38. Me.DGR_001.ItemsSource = ServerFolderTable.DefaultView
    39. End Sub
    40. Public Function GetColumns() As List(Of DataColumn)
    41. Dim DTSClm As New DataColumn("DataSource", System.Type.GetType("System.String"))
    42. Dim FPClm As New DataColumn("FullPath", System.Type.GetType("System.String"))
    43. Dim UserName As New DataColumn("UserName", System.Type.GetType("System.String"))
    44. Dim UserPW As New DataColumn("PDM_PW", System.Type.GetType("System.String"))
    45. Dim ReturningColumnsList As New List(Of DataColumn) From {DTSClm, UserName, UserPW, FPClm}
    46. Return ReturningColumnsList
    47. End Function
    48. Public Sub ChangeColumns()
    49. For Each CLMTemp As DataGridColumn In Me.DGR_001.Columns
    50. If CLMTemp.Header.ToString = "DataSource" Then
    51. CLMTemp.Header = "Quelle"
    52. CLMTemp.DisplayIndex = 0
    53. CLMTemp.Width = 100
    54. ElseIf CLMTemp.Header.ToString = "FullPath" Then
    55. CLMTemp.Header = "Quell Details"
    56. CLMTemp.DisplayIndex = 3
    57. CLMTemp.Width = New DataGridLength(1, DataGridLengthUnitType.Star)
    58. ElseIf CLMTemp.Header.ToString = "UserName" Then
    59. CLMTemp.Header = "User"
    60. CLMTemp.DisplayIndex = 1
    61. CLMTemp.Width = 100
    62. ElseIf CLMTemp.Header.ToString = "PDM_PW" Then
    63. CLMTemp.Header = "User PW"
    64. CLMTemp.DisplayIndex = 2
    65. CLMTemp.Width = 150
    66. End If
    67. Next
    68. End Sub
    69. Public Function GetFolderTree(ByVal Main_Folder As String, Optional ByVal PDMImage As Boolean = True) As FolderTree
    70. Dim Returning_FolderTree As FolderTree = New FolderTree
    71. Dim Temp_DirectoryInfo As IO.DirectoryInfo = New IO.DirectoryInfo(Main_Folder)
    72. If PDMImage Then
    73. Returning_FolderTree.Folder = Main_Folder
    74. Returning_FolderTree.FullName = Temp_DirectoryInfo.FullName
    75. Returning_FolderTree.Checked = False
    76. Returning_FolderTree.Image = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("SW_PDM_Icon"), System.Drawing.Image))
    77. Else
    78. Returning_FolderTree.Folder = Temp_DirectoryInfo.Name
    79. Returning_FolderTree.FullName = Temp_DirectoryInfo.FullName
    80. Returning_FolderTree.Checked = False
    81. Returning_FolderTree.Image = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Folder_Closed"), System.Drawing.Image))
    82. End If
    83. If Get_Files Then
    84. End If
    85. For Each SubFolder As IO.DirectoryInfo In Temp_DirectoryInfo.GetDirectories
    86. Returning_FolderTree.SubItems.Add(GetFolderTree(SubFolder.FullName, False))
    87. Next
    88. Return Returning_FolderTree
    89. End Function
    90. Public Sub TreeViewItem_Expanded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    91. 'TODO: Ändern Bild wenn Treenode is expanded
    92. If TreeViewUpdate = False Then
    93. Dim Item As System.Windows.Controls.TreeViewItem = CType(e.OriginalSource, System.Windows.Controls.TreeViewItem)
    94. If Item.IsExpanded Then
    95. Dim temp As FolderTree = CType(Item.DataContext, FolderTree)
    96. temp.IsExpanded_hnd = Item.IsExpanded
    97. If Not temp.Folder Like "*GOK_SW_PDM*" And Not temp.Folder Like "*C:\GOK_TEST_PDM\*" Then
    98. temp.Image = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Folder_Open"), System.Drawing.Image))
    99. End If
    100. ElseIf Item.IsExpanded = False Then
    101. Dim temp As FolderTree = CType(Item.DataContext, FolderTree)
    102. temp.IsExpanded_hnd = Item.IsExpanded
    103. If Not temp.Folder Like "*GOK_SW_PDM*" Or Not temp.Folder Like "*C:\GOK_TEST_PDM\*" Then
    104. temp.Image = GOKMF_Image_Functions.ConvertImageToImagesource(DirectCast(My.Resources.ResourceManager.GetObject("Folder_Closed"), System.Drawing.Image))
    105. End If
    106. End If
    107. RefreshTreeview()
    108. End If
    109. End Sub
    110. Public Function GetSelectedTreeViewItemParent(ByVal item As TreeViewItem) As ItemsControl
    111. Dim parent As DependencyObject = System.Windows.Media.VisualTreeHelper.GetParent(item)
    112. While Not (TypeOf parent Is TreeViewItem OrElse TypeOf parent Is System.Windows.Controls.TreeView)
    113. parent = System.Windows.Media.VisualTreeHelper.GetParent(parent)
    114. End While
    115. Return TryCast(parent, ItemsControl)
    116. End Function
    117. Private Sub Folder_Selection_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    118. ChangeColumns()
    119. End Sub
    120. Private Sub BTN_Refresh_Tabel_Click(sender As Object, e As RoutedEventArgs) Handles BTN_Refresh_Tabel.Click
    121. ServerFolderTable.Rows.Clear()
    122. For Each SubItem As FolderTree In Me.FolderTreeView.Items
    123. AddnewCheckedFolderTreeItems(SubItem)
    124. Next
    125. End Sub
    126. Private Sub AddnewCheckedFolderTreeItems(ByVal SelectedItem As FolderTree)
    127. If SelectedItem.Checked Then
    128. Dim NewRow As DataRow = ServerFolderTable.NewRow
    129. NewRow.BeginEdit()
    130. NewRow("FullPath") = SelectedItem.FullName
    131. NewRow.EndEdit()
    132. ServerFolderTable.Rows.Add(NewRow)
    133. End If
    134. If Not SelectedItem.SubItems Is Nothing Then
    135. For Each SubItem As FolderTree In SelectedItem.SubItems
    136. AddnewCheckedFolderTreeItems(SubItem)
    137. Next
    138. End If
    139. End Sub
    140. Private Sub BTN_Select_All_Click(sender As Object, e As RoutedEventArgs) Handles BTN_Select_All.Click
    141. Dim SelectedItem As FolderTree = CType(Me.FolderTreeView.SelectedItem, FolderTree)
    142. SelectedItem.Checked = True
    143. CheckkAllSubItems(SelectedItem)
    144. RefreshTreeview()
    145. End Sub
    146. Private Sub CheckkAllSubItems(ByVal SelectedItem As FolderTree)
    147. For Each SubItem As FolderTree In SelectedItem.SubItems
    148. SubItem.Checked = True
    149. If Not SubItem.SubItems Is Nothing Then
    150. CheckkAllSubItems(SubItem)
    151. End If
    152. Next
    153. End Sub
    154. Private Sub RefreshTreeview()
    155. TreeViewUpdate = True
    156. Me.FolderTreeView.Items.Refresh()
    157. Me.FolderTreeView.UpdateLayout()
    158. TreeViewUpdate = False
    159. End Sub
    160. End Class
    161. Public Class FolderTree
    162. Public Property Image As System.Windows.Media.Imaging.BitmapImage
    163. Public Property Folder As String
    164. Public Property FullName As String
    165. Public Property IsExpanded_hnd As Boolean = False
    166. Public Property Checked As Boolean
    167. Public ReadOnly Property SubItems As List(Of FolderTree) = New List(Of FolderTree)()
    168. End Class