2013年1月6日日曜日

コンテンツ(ListBox基本)


ListBoxクラス

クラスの定義は以下の通り

        Module Name: PresentationFramework.dll

  System.Windows.Controls.ListBox
    System.Windows.Controls.Primitives.Selector
      System.Windows.Controls.ItemsControl <--これから派生されている(ItemsとItemsSrouceを保持)
        System.Windows.Controls.Control
          System.Windows.FrameworkElement
            System.Windows.UIElement
              System.Windows.Media.Visual
                System.Windows.DependencyObject
                  System.Windows.Threading.DispatcherObject
                    System.Object


実装にあたって2つの形式がある

①ListBoxItemとして保持する(ラップする)
②直接配列として保持する


実際に使ってみる

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

ListBoxItemにラップする形式と直接配列保存の形式
 ListBoxの動作確認
#-------------------------------------------------------
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">

<ListBox>
   <ListBoxItem>徳川家康</ListBoxItem>
   <ListBoxItem>織田信長</ListBoxItem>
   <ListBoxItem>豊臣秀吉</ListBoxItem>
</ListBox>
<!--
<ListBox>
<TextBlock>徳川家康</TextBlock>
<TextBlock>織田信長</TextBlock>
<Button>豊臣秀吉</Button>
</ListBox>
-->
</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
#-------------------------------------------------------

保管データの確認

  • 形式1(ListBoxItemでカプセル)

> 0..2 | % { $form.Content.Items[$_].content }
徳川家康
織田信長
豊臣秀吉

  • 形式2(直接的に配列保管)

> 0..2 | % { $form.content.items[$_].gettype() } | ft Name -AutoSize
Name
----
TextBlock
TextBlock
Button

0 件のコメント:

コメントを投稿