2017-02-22 126 views
2

我無法使虛擬化在Popup內的TreeView上工作,而同一TreeView完美工作在Popup之外。Popup中的UI虛擬化

<Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <ToggleButton IsChecked="{Binding ElementName=Popup, Path=IsOpen, Mode=TwoWay}" 
         Content="Test"/> 

     <TreeView Grid.Row="1" ItemsSource="{Binding Nodes}" VirtualizingPanel.IsVirtualizing="True"> 
      <TreeView.ItemTemplate> 
       <HierarchicalDataTemplate ItemsSource="{Binding Children}"> 
        <TextBlock Text="{Binding Data}"/> 
       </HierarchicalDataTemplate> 
      </TreeView.ItemTemplate> 
     </TreeView> 

     <Popup x:Name="Popup" StaysOpen="True"> 
      <TreeView ItemsSource="{Binding Nodes}" VirtualizingPanel.IsVirtualizing="True"> 
       <TreeView.ItemTemplate> 
        <HierarchicalDataTemplate ItemsSource="{Binding Children}"> 
         <TextBlock Text="{Binding Data}"/> 
        </HierarchicalDataTemplate> 
       </TreeView.ItemTemplate> 
      </TreeView> 
     </Popup> 
    </Grid> 

視圖模型和Node類

public class ViewModel 
{ 
    public ObservableCollection<Node> Nodes { get; private set; } 
    public ViewModel() 
    { 
     Nodes = new ObservableCollection<Node>(); 
     Node parent = new Node("Parent"); 

     for (int i = 0; i < 10000; i++) 
      parent.Children.Add(new Node(i.ToString())); 

     Nodes.Add(parent); 
    } 
} 
public class Node 
{ 
    public string Data { get; set; } 

    public List<Node> Children { get; set; } 
    public Node(string data) 
    { 
     Data = data; 
     Children = new List<Node>(); 
    } 
} 

當我展開窗口裏面不花時間,同時擴大內彈出一個需要〜20多歲的樹父。

MS確認這是2010s中的一個錯誤,並建議使用ControlTemplate將ToggleButton和TreeView合併到一個控件中,但它對我沒有任何影響。

回答

1

想通了,所有你需要做的就是指定一個最大高度(正常高度的作品太)在彈出

<Popup x:Name="Popup" StaysOpen="True" MaxHeight="500"> 

真的不知道爲什麼這會有所幫助。也許WPF需要做太多的大小計算,如果它的自動。