2013年1月6日日曜日

コンテンツ(Expander)

HeaderedContentControlクラス

クラス定義は、

    Module Name: PresentationFramework.dll

System.Windows.Controls.HeaderedContentControl
 System.Windows.Controls.ContentControl         <--これの拡張
   System.Windows.Controls.Control
     System.Windows.FrameworkElement
       System.Windows.UIElement
         System.Windows.Media.Visual
           System.Windows.DependencyObject
             System.Windows.Threading.DispatcherObject
               System.Object

 このクラスはコンテンツを保持するプロパティを2つ持つ

    「ContentControl」クラスが元々持っているもの
        >   [System.Windows.Controls.ContentControl]::System.Object get_Content()
        >   [System.Windows.Controls.ContentControl]::Void set_Content(System.Object)
        >   [System.Windows.Controls.ContentControl]::System.Object Content
        >   [System.Windows.Controls.ContentControl]::System.Windows.DependencyProperty ContentProperty

    「HearderedCotentControl」クラスで追加されたもの(上記Contentのタイトル表記)
        >   [System.Windows.Controls.HeaderedContentControl]::Void set_Header(System.Object)
        >   [System.Windows.Controls.HeaderedContentControl]::Boolean get_HasHeader()
        >   [System.Windows.Controls.HeaderedContentControl]::System.Object Header
   >   [System.Windows.Controls.HeaderedContentControl]::System.Windows.DependencyProperty HeaderProperty


「HearderedCotentControl」から派生するクラスの一覧は?

        IsPublic IsSerial Name     BaseType
        -------- -------- ----     --------
        True     False    GroupBox System.Windows.Controls.HeaderedContentControl
        True     False    Expander System.Windows.Controls.HeaderedContentControl
        True     False    TabItem  System.Windows.Controls.HeaderedContentControl


Expanderクラスについて

    以下の2つを同時に実現
    ・1つのコンテンツを保持する
    ・コンテンツを表示/非表示を切り替えられる


実際に使ってみる

  1. PowershellだけでWPF表示(Powershell3.0でも2.0でも稼働)
  2. .Netframeworkは3.0以上が必要
  3. WPFの約束としてPowershellはSTAモードで動く必要あり
  4. 以下コードを
      ①Powershellコンソールにコピペして実行
      ②ISEで実行する時はすでにSTAモードなので適時修正して実行
        注意:ブラウザによってはコピペで行頭に余分な空白が出来てしまうことあり
               C:\temp> □'@   ← ここの行の行頭に空白あると動かない 

折りたたんだ状態と表示した状態
Expanderの動作確認
#-------------------------------------------------------
if ($host.Version.Major -eq 3) {
    powershell.exe
} else {
    if ($host.Runspace.ApartmentState -eq "STA") {return}
    powershell.exe -version 2 -sta
}

$xaml_win = @'
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Expander調査" Height="220" Width="250">

<Grid>
<Expander Header="Important Buttons">
<StackPanel>
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
</StackPanel>
</Expander>
</Grid>
</Window>
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

#-------------------------------------------------------
if ($host.Version.Major -eq 3) {
Add-Type -AssemblyName WindowsBase,PresentationCore,PresentationFramework,system.xml,System.Xaml
} else {
Add-Type -AssemblyName WindowsBase,PresentationCore,PresentationFramework,system.xml
}
$form = [System.Windows.Markup.XamlReader]::Parse($xaml_win)
[void]$form.showdialog()
exit

0 件のコメント:

コメントを投稿