次の方法で共有


エキスパンダーの概要

Expander コントロールでは、ウィンドウに似た、ヘッダーを持つ展開可能な領域内にコンテンツを表示する手段が提供されます。

単純なエキスパンダーの作成

次の例では、単純な Expander コントロールの作成方法を示します。 この例では、前の図のような外観の Expander を作成します。

<Expander Name="myExpander" Background="Tan" 
          HorizontalAlignment="Left" Header="My Expander" 
          ExpandDirection="Down" IsExpanded="True" Width="100">
  <TextBlock TextWrapping="Wrap">
    Lorem ipsum dolor sit amet, consectetur
    adipisicing elit, sed do eiusmod tempor incididunt ut
    labore et dolore magna aliqua
  </TextBlock>
</Expander>

ExpanderContent および Header には、RadioButton オブジェクトや Image オブジェクトなどの複雑なコンテンツも格納できます。

コンテンツ エリアの展開方向の設定

ExpandDirection プロパティを使用することにより、Expander コントロールのコンテンツ領域を 4 つの方向 (DownUpLeftRight) のいずれかに展開するように設定できます。 コンテンツ領域が折りたたまれると、ExpanderHeader とそのトグル ボタンのみが表示されます。 コンテンツ領域を展開したり、折りたたんだりするトグル ボタンとしては、方向矢印を表示する Button コントロールが使用されます。 展開時には、Expander により、ウィンドウに似た領域内にそのすべてのコンテンツの表示が試みられます。

パネル内のエキスパンダーのサイズの制御

Expander コントロールが Panel から継承される StackPanel などのレイアウト コントロールの内側にあり、ExpandDirection プロパティが Down または Up に設定されている場合は、Expander に対して Height を指定しないでください。 同様に、ExpandDirection プロパティが Left または Right に設定されている場合は、Expander に対して Width を指定しないでください。

展開されたコンテンツが表示される方向のサイズを Expander コントロールに対して設定すると、コンテンツで使用される領域が Expander によって制御され、その周囲に境界線が表示されます。 コンテンツが折りたたまれても、境界線は表示されます。 展開されたコンテンツ エリアのサイズを設定するには、Expander のコンテンツに対してサイズを設定します。スクロール可能にする場合は、コンテンツを囲む ScrollViewer に対してサイズを設定します。

Expander コントロールが DockPanel の最後の要素である場合は、DockPanel の残りの領域に等しくなるように、Expander のサイズが Windows Presentation Foundation (WPF) によって自動的に設定されます。 この既定の動作を回避するには、DockPanel オブジェクトの LastChildFill プロパティを false に設定するか、ExpanderDockPanel の最後の要素にならないようにします。

スクロール可能なコンテンツの作成

コンテンツが大きすぎて、コンテンツ領域に表示できない場合は、Expander のコンテンツを ScrollViewer 内にラップすることで、コンテンツをスクロール可能にすることができます。 Expander コントロールでは、スクロール機能は自動的には提供されません。 次の図では、ScrollViewer コントロールが含まれる Expander コントロールを示します。

ScrollViewer 内のエキスパンダー

Screenshot that shows an expander with ScrollBar.

Expander コントロールを ScrollViewer 内に配置する場合は、Expander のコンテンツが開く方向に対応する ScrollViewer サイズ プロパティを、Expander のコンテンツ領域のサイズに設定します。 たとえば、ExpanderExpandDirection プロパティを Down (コンテンツ領域を下に開く) に設定する場合は、ScrollViewer コントロールの Height プロパティを、コンテンツ領域に必要な高さに設定します。 このように設定しないで、コンテンツ自体の高さにサイズを設定しても、この設定は ScrollViewer に認識されないため、コンテンツはスクロール可能になりません。

次の例では、コンテンツが複雑で、ScrollViewer コントロールが含まれる Expander コントロールを作成する方法を示します。 この例では、このセクションの冒頭で示した図のような Expander を作成します。


void MakeExpander()
{
  //Create containing stack panel and assign to Grid row/col
  StackPanel sp = new StackPanel();
  Grid.SetRow(sp, 0);
  Grid.SetColumn(sp, 1);
  sp.Background = Brushes.LightSalmon;

  //Create column title
  TextBlock colTitle = new TextBlock();
  colTitle.Text = "EXPANDER CREATED FROM CODE";
  colTitle.HorizontalAlignment= HorizontalAlignment.Center;
  colTitle.Margin.Bottom.Equals(20);
  sp.Children.Add(colTitle);

  //Create Expander object
  Expander exp = new Expander();

  //Create Bullet Panel for Expander Header
  BulletDecorator bp = new BulletDecorator();
  Image i = new Image();
  BitmapImage bi= new BitmapImage();
  bi.UriSource = new Uri(@"pack://application:,,/images/icon.jpg");
  i.Source = bi;
  i.Width = 10;
  bp.Bullet = i;
  TextBlock tb = new TextBlock();
  tb.Text = "My Expander";
  tb.Margin = new Thickness(20,0,0,0);
  bp.Child = tb;
  exp.Header = bp;

  //Create TextBlock with ScrollViewer for Expander Content
  StackPanel spScroll = new StackPanel();
  TextBlock tbc = new TextBlock();
  tbc.Text =
          "Lorem ipsum dolor sit amet, consectetur adipisicing elit," +
          "sed do eiusmod tempor incididunt ut labore et dolore magna" +
          "aliqua. Ut enim ad minim veniam, quis nostrud exercitation" +
          "ullamco laboris nisi ut aliquip ex ea commodo consequat." +
          "Duis aute irure dolor in reprehenderit in voluptate velit" +
          "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint" +
          "occaecat cupidatat non proident, sunt in culpa qui officia" +
          "deserunt mollit anim id est laborum.";
  tbc.TextWrapping = TextWrapping.Wrap;

  spScroll.Children.Add(tbc);
  ScrollViewer scr = new ScrollViewer();
  scr.Content = spScroll;
  scr.Height = 50;
  exp.Content = scr;

  exp.Width=200;
  exp.HorizontalContentAlignment= HorizontalAlignment.Stretch;
  //Insert Expander into the StackPanel and add it to the
  //Grid
  sp.Children.Add(exp);
  myGrid.Children.Add(sp);
}

Sub MakeExpander()

    'Create containing stack panel and assign to Grid row/col
    Dim sp As StackPanel = New StackPanel()
    Grid.SetRow(sp, 0)
    Grid.SetColumn(sp, 1)
    sp.Background = Brushes.LightSalmon

    'Create column title
    Dim colTitle As TextBlock = New TextBlock()
    colTitle.Text = "EXPANDER CREATED FROM CODE"
    colTitle.HorizontalAlignment = HorizontalAlignment.Center
    colTitle.Margin.Bottom.Equals(20)
    sp.Children.Add(colTitle)

    'Create Expander object
    Dim exp As Expander = New Expander()

    'Create Bullet Panel for Expander Header
    Dim bp As BulletDecorator = New BulletDecorator()
    Dim i As Image = New Image()
    Dim bi As BitmapImage = New BitmapImage()
    bi.UriSource = New Uri("pack://application:,,./images/icon.jpg")
    i.Source = bi
    i.Width = 10
    bp.Bullet = i
    Dim tb As TextBlock = New TextBlock()
    tb.Text = "My Expander"
    tb.Margin = New Thickness(20, 0, 0, 0)
    bp.Child = tb
    exp.Header = bp

    'Create TextBlock with ScrollViewer for Expander Content
    Dim spScroll As StackPanel = New StackPanel()
    Dim tbc As TextBlock = New TextBlock()
    tbc.Text = _
            "Lorem ipsum dolor sit amet, consectetur adipisicing elit," + _
            "sed do eiusmod tempor incididunt ut labore et dolore magna" + _
            "aliqua. Ut enim ad minim veniam, quis nostrud exercitation" + _
            "ullamco laboris nisi ut aliquip ex ea commodo consequat." + _
            "Duis aute irure dolor in reprehenderit in voluptate velit" + _
            "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint" + _
            "occaecat cupidatat non proident, sunt in culpa qui officia" + _
            "deserunt mollit anim id est laborum."
    tbc.TextWrapping = TextWrapping.Wrap

    spScroll.Children.Add(tbc)
    Dim scr As ScrollViewer = New ScrollViewer()
    scr.Content = spScroll
    scr.Height = 50
    exp.Content = scr

    'Insert Expander into the StackPanel and add it to the
    'Grid
    exp.Width = 200
    exp.HorizontalContentAlignment = HorizontalAlignment.Stretch
    sp.Children.Add(exp)
    myGrid.Children.Add(sp)
End Sub

<Expander Width="200" HorizontalContentAlignment="Stretch">
   <Expander.Header>
     <BulletDecorator>
       <BulletDecorator.Bullet>
         <Image Width="10" Source="images\icon.jpg"/>
       </BulletDecorator.Bullet>
       <TextBlock Margin="20,0,0,0">My Expander</TextBlock>
     </BulletDecorator>
   </Expander.Header>
   <Expander.Content>
     <ScrollViewer Height="50">
       <TextBlock TextWrapping="Wrap">
         Lorem ipsum dolor sit amet, consectetur adipisicing elit, 
         sed do eiusmod tempor incididunt ut labore et dolore magna 
         aliqua. Ut enim ad minim veniam, quis nostrud exercitation 
         ullamco laboris nisi ut aliquip ex ea commodo consequat. 
         Duis aute irure dolor in reprehenderit in voluptate velit 
         esse cillum dolore eu fugiat nulla pariatur. Excepteur sint 
         occaecat cupidatat non proident, sunt in culpa qui officia 
         deserunt mollit anim id est laborum.
       </TextBlock>
     </ScrollViewer>
   </Expander.Content>
 </Expander>


配置プロパティの使用

コンテンツの配置を設定するには、Expander コントロールの HorizontalContentAlignment プロパティと VerticalContentAlignment プロパティを設定します。 これらのプロパティを設定すると、配置がヘッダーに適用され、展開されたコンテンツにも適用されます。

関連項目