2010-01-29 63 views
1

我是WPF新手,我試圖使用擴展器構建下拉菜單。頁面佈局正在使用Grid進行處理。如何在不擴展當前行的情況下將WPF GridRow內容擴展到下一行

擴展器位於網格的第一行內,我希望擴展器的內容在點擊時展開在下面所有內容的頂部。不幸的是,現在,整行被擴展以適應擴展控件的高度。

我試着玩的ZIndex,但它似乎沒有任何效果。無論如何,該行總是擴展,強制頁面上的其他所有內容移動。

<Expander FontSize="18" Name="moduleSelect" Width="100" Header=" Goto -> " 
      Background="#000033" MouseEnter="moduleSelect_MouseEnter" 
      MouseLeave="moduleSelect_MouseLeave" Foreground="White" 
      Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Left"> 
    <StackPanel> 
     <Button Name="btnTasks" Width="100" Foreground="White" 
       Background="#000033">Tasks</Button> 
     <Button Name="btnNotes" Width="100" Foreground="White" 
       Background="#000033">Notes</Button> 
    </StackPanel> 
</Expander> 

如何在不扭曲網格的情況下在後續行上方展開'上方'?

回答

1

如果將Grid.RowSpanExpander設置爲2(或者希望展開的行數是多少),會發生什麼情況?

因此,對於兩行的網格,你有這樣的事情:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="30" /> <!--set this to the height of the expander's header area--> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
    <WhateverGoesInRow2 Grid.Row="1" /> 
    <Expander FontSize="18" Name="moduleSelect" Width="100" Header=" Goto -> " 
     Background="#000033" MouseEnter="moduleSelect_MouseEnter" 
     MouseLeave="moduleSelect_MouseLeave" Foreground="White" 
     Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Left" 
     Grid.Row=0 Grid.RowSpan="2"> 
     <StackPanel> 
      <Button Name="btnTasks" Width="100" Foreground="White" Background="#000033">Tasks</Button> 
      <Button Name="btnNotes" Width="100" Foreground="White" Background="#000033">Notes</Button> 
     </StackPanel> 
    </Expander> 
</Grid> 

您可能需要調整您的RowDefinition部分特定的情形,但如果我理解正確的問題,我認爲這會起作用。

+0

擴展器打開時,不會將列中的所有內容都壓低一行嗎? – 2010-01-29 21:54:51

+0

@Robert,它不應該。這就是整個點'Grid.RowSpan =「2」'。它使一個對象佔據'Grid'的兩行。通過將網格的高度設置爲標題的高度,當您打開擴展器時,所有溢出(彈出的東西)將進入第二行。 – devuxer 2010-01-29 22:14:16

+0

@羅伯特,我只是對我的答案做了一個小小的修改。我認爲Z順序依賴於你先放在你的XAML中(儘管你可以重寫它,如果你願意的話),所以我把「WhateverGoesInRow2」放在擴展器之前。這應該使擴展器在打開時出現在其他東西上方。 – devuxer 2010-01-29 22:19:05

0

內置下拉控件利用其默認控件模板中的Popup控件來做類似的事情。

1

你想要在網格上彈出一些東西,而不是在網格內展開。 A ComboBox,或者 - 畢竟這是一個菜單 - 一個ContextMenu

你也可以建立一個ToggleButtonPopup的某種組合,但本質上是一樣的東西與IsEditable一個ComboBox關閉。

相關問題