2015-05-20 34 views
1

我遇到了問題,我想將我的TreeView的最大高度綁定到UserControl的高度,但綁定不起作用。將XAML TreeView MaxHeight綁定到容器高度

我嘗試以下

<UserControl> 
    <StackPanel Name="Container"> 
     <TextBlock>Header</TextBlock> 
     <TreeView MaxHeight="{Binding ElementName=Container,Path=ActualHeight}"></TreeView> 
    </StackPanel> 
</UserControl> 

我所期望的,如果我調整我的窗口,用戶控件調整大小等TreeView中被調整過,因此,如果窗口是小,TreeView的,滾動條出現。 但我得到的是沒有滾動條和TreeView的內容到達窗口之外,並且不可見。

回答

1

這是一個很荒唐的常見錯誤。 StackPanel確實不是具有任何調整大小的功能,只應用於最基本的佈局目的。相反,使用Grid Panel,它會自動調整其子控件:

<UserControl> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto" /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <TextBlock>Header</TextBlock> 
     <TreeView Grid.Row="1" /> 
    </Grid> 
</UserControl> 
+0

哈,是的,太荒謬了。大聲笑!!! :-) – user2697817

+0

感謝您的快速回放和有用的答案。我用Grid元素解決了這個問題。一個小建議:你的文章中的「Grid.Column」定義必須是「Grid.Row」,但不是它的工作。 – bestfansdynamo

+0

感謝您的通知,@bestfansdynamo ...我已經相應地更新了我的答案。 – Sheridan

0

您可以使用DockPanel

<DockPanel LastChildFill="True"> 
    <TextBlock DockPanel.Dock="Top">Header</TextBlock> 
    <TreeView DockPanel.Dock="Top" MaxHeight="{Binding ElementName=Container,Path=ActualHeight}"></TreeView> 
</DockPanel> 
+0

'DockPanel'是相當重的重量控制,只能在需要確切的功能時使用。 「Grid」的重量要輕得多,所以在這種情況下更好。 – Sheridan