2013-12-13 71 views
0

所以,我有一個樹狀圖,我有一個列表。我想對它應用一個過濾器,並且所有不符合過濾器上單詞的節點都會崩潰,所以它們不佔用空間。WPF摺疊TreeView中的節點

的問題是,即使我將它們設置爲崩潰,他們仍然需要最小的空間量...

 <TreeView x:Name="UserTree"> 
      <TreeView.Resources> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
    Color="Transparent" /> 
       <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" 
    Color="Black" /> 
      <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" 
    Color="Transparent" /> 
      <local:SumConverter x:Key="sumConverter"/> 
     </TreeView.Resources> 
     <TreeView.ItemTemplate> 
      <HierarchicalDataTemplate ItemsSource="{Binding UserList}"> 
       <Border x:Name="headerTree" Background="#FFD3D1D1" CornerRadius="3" Padding="2" 
    Margin="2"> 
        <Grid Width="330" Height="20"> 
         <TextBlock Text="{Binding Name}" Name="txtGroupName" Width="280" 
    Height="20" FontFamily="Palatino Linotype" Foreground="Black" VerticalAlignment="Center" 
    AllowDrop="True" Drop="TextBlock_Drop" Padding="5,0,0,0" HorizontalAlignment="Left" 
    Margin="0,2,0,0" /> 
         <CheckBox x:Name="cbSelector" HorizontalAlignment="Right" 
    VerticalAlignment="Center" Margin="0,0,5,0" Checked="cbSelector_Checked" 
    Unchecked="cbSelector_Unchecked" /> 
        </Grid> 
        </Border> 
        <HierarchicalDataTemplate.ItemTemplate> 
         <DataTemplate> 
          <Grid Width="320" Visibility="{Binding isVisible}"> 
           <CheckBox Margin="16,1,1,1" VerticalAlignment="center" 
    Checked="ChkUser_Checked" Unchecked="chkUser_Unchecked" 
            IsChecked="{Binding IsListed}" 
    PreviewMouseMove="CheckBox_PreviewMouseMove" 
            PreviewMouseLeftButtonDown="CheckBox_PreviewMouseLeftButtonDown" ToolTipService.ToolTip=" 
    {Binding Section}"> 
            <CheckBox.Content> 
             <MultiBinding Converter="{StaticResource sumConverter}"> 
              <Binding Path="Name"/> 
              <Binding Path="Status"/> 
             </MultiBinding> 
            </CheckBox.Content> 
           </CheckBox> 
           <Image Source="{Binding ImagemStatus}" Width="16" Height="16" 
    HorizontalAlignment="Left" Margin="0,2,0,0" /> 
           <Button x:Name="btnRemoveFromGroup"  Click="btnRemoveFromGroup_Click" HorizontalAlignment="Right" Width="16px" Height="16px" 
VerticalAlignment="Top" Style="{StaticResource MyButtonStyle}" Foreground="{x:Null}" 
ToolTipService.ToolTip="Remove from group" Content=" 
    {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type 
    TreeViewItem}, AncestorLevel=2}, Path=DataContext.ObtainGroup}"> 
            <Button.Background> 
             <ImageBrush ImageSource="Imagens/Grp.png"/> 
            </Button.Background> 
           </Button> 
          </Grid> 
         </DataTemplate> 
        </HierarchicalDataTemplate.ItemTemplate> 
       </HierarchicalDataTemplate> 
      </TreeView.ItemTemplate> 
      <TreeView.ItemContainerStyle> 
       <Style TargetType="{x:Type TreeViewItem}"> 
        <Setter Property="IsExpanded" Value="{Binding IsExpanded}"></Setter> 
       </Style> 
      </TreeView.ItemContainerStyle> 
     </TreeView> 

<Style x:Key="MyButtonStyle" TargetType="Button"> 
      <Setter Property="OverridesDefaultStyle" Value="True"/> 
      <Setter Property="Margin" Value="5"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border Name="border" 
          BorderThickness="0" 
          Padding="4,2" 
          BorderBrush="DarkGray" 
          CornerRadius="3" 
          Background="{TemplateBinding Background}"> 
          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsMouseOver" Value="True"> 
           <Setter TargetName="border" Property="BorderBrush" Value="Black" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
+0

摺疊不會從實例(從內存)中刪除它們,您將不得不處置這些節點的實例 – Shaharyar

+0

因此,擺脫空間的唯一方法是將它們移動到另一個列表(例如)或刪除它? – HDD

+0

移動他們不會將他們從內存中刪除,你將不得不處置 – Shaharyar

回答

0

當你曾經啓動一個進程,它需要一些內存空間。當你創建一個實例時,它也會在內存中創建。將數據從一個實例移動到另一個實例不會釋放內存。你必須處理這個對象來釋放它。

好吧!在你的情況下,你在treeview中有strings作爲節點。一個treeviewNode存儲一些額外的信息。如果您不想delete節點,那麼您有另一種選擇。

您可以將它保存在string類型list中,並且可以將dispose保存爲treeview的那個節點。 A list不存儲太多的信息,因爲node。所以它會爲你節省一些記憶。

+0

垃圾回收器釋放內存。處置是爲了清理GC所不能處理的類所包含的任何資源。有許多文章討論GC和Dispose模式如何工作。我建議你看這個。 –

0

你正在攻擊錯誤的問題。你應該在你的對象上設置一個遞歸函數,在每個集合上使用CollectionViewSource.GetDefaultView,並在每個集合上設置Filter。然後這個過濾器會從樹形視圖中移除這些項目,但不會實際刪除它們。然後,您可以遞歸地將篩選器設置爲空,並且所有項目都將再次可見。

0

我覺得你的問題可以用下面的代碼來解決:

<TreeView x:Name="UserTree"> 
    <TreeView.ItemContainerStyle> 
     <Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource {x:Type TreeViewItem}}"> 
      <Setter Property="Visibility" Value="{Binding IsVisible}" /> 
     </Style> 
     </TreeView.ItemContainerStyle> 

     ... 

</TreeView> 

在你的類「UserList的」必須聲明一個名爲「可見性」屬性。當將此屬性設置爲「摺疊」時,treeviewitem不會在樹視圖中佔用任何空間