WPF——Expander控件(轉)


Expander是一個可以展開和折疊的控件,它包含兩部分標頭和內容。 標頭通Header屬性來設置,內容通過Conent屬性設置,如下面一個簡單的例子: Expander ExpandDirection = Down Width = 96 Expander.Header TextBlock Text = 標題 FontWeight = Bold / / Expander.Header Expander.Content TextBlock TextWrapping = Wrap T
  Expander是一個可以展開和折疊的控件,它包含兩部分——標頭和內容。

  標頭通Header屬性來設置,內容通過Conent屬性設置,如下面一個簡單的例子:

  1. <Expander ExpandDirection="Down" Width="96"> 
  2.     <Expander.Header> 
  3.         <TextBlock Text="標題" FontWeight="Bold"/> 
  4.     </Expander.Header> 
  5.     <Expander.Content> 
  6.         <TextBlock TextWrapping="Wrap"  Text="這里是內容。"/> 
  7.     </Expander.Content> 
  8. </Expander> 

  Expander控件的Header和Content都可以為任何對象,只要能正常顯示即可。

  下面就是該控件運行時的截圖。

  

 
 

 

  這個控件最實用的地方,就是做導航欄。

  1. <StackPanel Margin="20,20" Width="100" Height="460" HorizontalAlignment="Left" 
  2.             VerticalAlignment="Top"> 
  3.     <Expander VerticalAlignment="Stretch" ExpandDirection="Down" HorizontalContentAlignment="Left" SnapsToDevicePixels="True"> 
  4.         <Expander.Header> 
  5.             <TextBlock Text="用戶管理" FontSize="14" FontWeight="Bold" /> 
  6.         </Expander.Header> 
  7.         <Expander.Content> 
  8.             <Grid> 
  9.                 <Grid.RowDefinitions> 
  10.                     <RowDefinition Height="auto"/> 
  11.                     <RowDefinition Height="auto"/> 
  12.                 </Grid.RowDefinitions> 
  13.                 <RadioButton Grid.Row="0">會員管理</RadioButton> 
  14.                 <RadioButton Grid.Row="1">角色管理</RadioButton> 
  15.             </Grid> 
  16.         </Expander.Content> 
  17.     </Expander> 
  18.     <Expander VerticalAlignment="Stretch" ExpandDirection="Down" HorizontalContentAlignment="Left" SnapsToDevicePixels="True"> 
  19.         <Expander.Header> 
  20.             <TextBlock Text="文檔管理" FontSize="14" FontWeight="Bold" /> 
  21.         </Expander.Header> 
  22.         <Expander.Content> 
  23.             <Grid> 
  24.                 <Grid.RowDefinitions> 
  25.                     <RowDefinition Height="auto"/> 
  26.                     <RowDefinition Height="auto"/> 
  27.                     <RowDefinition Height="auto"/> 
  28.                 </Grid.RowDefinitions> 
  29.                 <RadioButton Grid.Row="0">部門資料</RadioButton> 
  30.                 <RadioButton Grid.Row="1">員工資料</RadioButton> 
  31.                 <RadioButton Grid.Row="2">職位資料</RadioButton> 
  32.             </Grid> 
  33.         </Expander.Content> 
  34.     </Expander> 
  35.     <Expander VerticalAlignment="Stretch" ExpandDirection="Down" HorizontalContentAlignment="Left" SnapsToDevicePixels="True"> 
  36.         <Expander.Header> 
  37.             <TextBlock Text="采購管理" FontSize="14" FontWeight="Bold" /> 
  38.         </Expander.Header> 
  39.         <Expander.Content> 
  40.             <Grid> 
  41.                 <Grid.RowDefinitions> 
  42.                     <RowDefinition Height="auto"/> 
  43.                     <RowDefinition Height="auto"/> 
  44.                     <RowDefinition Height="auto"/> 
  45.                     <RowDefinition Height="auto"/> 
  46.                     <RowDefinition Height="auto"/> 
  47.                 </Grid.RowDefinitions> 
  48.                 <RadioButton Grid.Row="0">采購計划</RadioButton> 
  49.                 <RadioButton Grid.Row="1">需求分析</RadioButton> 
  50.                 <RadioButton Grid.Row="2">采購單</RadioButton> 
  51.                 <RadioButton Grid.Row="3">入庫驗收</RadioButton> 
  52.                 <RadioButton Grid.Row="4">入庫退回</RadioButton> 
  53.             </Grid> 
  54.         </Expander.Content> 
  55.     </Expander> 
  56.     <Expander VerticalAlignment="Stretch" ExpandDirection="Down" HorizontalContentAlignment="Left" SnapsToDevicePixels="True"> 
  57.         <Expander.Header> 
  58.             <TextBlock Text="供應商" FontSize="14" FontWeight="Bold" /> 
  59.         </Expander.Header> 
  60.         <Expander.Content> 
  61.             <Grid> 
  62.                 <Grid.RowDefinitions> 
  63.                     <RowDefinition Height="auto"/> 
  64.                     <RowDefinition Height="auto"/> 
  65.                     <RowDefinition Height="auto"/> 
  66.                 </Grid.RowDefinitions> 
  67.                 <RadioButton Grid.Row="0">基本資料</RadioButton> 
  68.                 <RadioButton Grid.Row="1">往來單位</RadioButton> 
  69.                 <RadioButton Grid.Row="2">上游供應商</RadioButton> 
  70.             </Grid> 
  71.         </Expander.Content> 
  72.     </Expander> 
  73. </StackPanel> 

  

 

 

  另外,通過ExpandDirection屬性可控其展開的方向。

  1. <Expander VerticalAlignment="Stretch" ExpandDirection="Right" HorizontalContentAlignment="Left" SnapsToDevicePixels="True"> 
  2.          ............ 
  3.  
  4. </Expander> 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM