2013年1月3日木曜日

レイアウト(Grid:共有グループ)


Panelはエレメントを種々の形に並べて配置するコンテナの役割をするエレメント

Panelから拡張されるクラスの一覧は以下の通り

 > [System.Windows.Controls.Panel]  をBaseTypeとするもの

  IsPublic IsSerial Name                        BaseType
  --------   --------  ----                             --------
  True     False    StackPanel                 System.Windows.Controls.Panel     積み重ね配置
  True     False    WrapPanel                  System.Windows.Controls.Panel     次の行/列へ折り返す配置
  True     False    DockPanel                  System.Windows.Controls.Panel     互いに引っ付いた配置
  True     False    Grid                             System.Windows.Controls.Panel     格子状の配置
  True     False    Canvas                       System.Windows.Controls.Panel     画布に描く配置
  True     False    UniformGrid                 System.Windows.Controls.Panel     Gridの簡素なやつ
  True     False    TabPanel                    System.Windows.Controls.Panel     WebのTab的な配置
  True     False    ToolBarOverflowPanel System.Windows.Controls.Panel     WebのToolBar的
  True     False    VirtualizingPanel           System.Windows.Controls.Panel     Webの縦Panel的


共有サイズグループの指定

  • Gridの定義用クラスに「SharedSizeGroup」機能というのがある

    同じ名前のグループ同士のCellは、同じサイズにリアルタイムでサイズを連動させてくれる。


  • 共有サイズグループの有効化
                System.Type : System.Windows.Controls.Grid

        [System.Windows.Controls.Grid]::Void SetIsSharedSizeScope(System.Windows.UIElement, Boolean)
        [System.Windows.Controls.Grid]::Boolean GetIsSharedSizeScope(System.Windows.UIElement)
        [System.Windows.Controls.Grid]::System.Windows.DependencyProperty IsSharedSizeScopeProperty


  • 「SharedSizeGroup」プロパティ
              System.Type : System.Windows.Controls.ColumnDefinition

        [System.Windows.Controls.DefinitionBase]::System.String get_SharedSizeGroup()
        [System.Windows.Controls.DefinitionBase]::Void set_SharedSizeGroup(System.String)
       [System.Windows.Controls.DefinitionBase]::System.String SharedSizeGroup



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


「A」と「D」が共有サイズグループ
---------------------------------------------
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="GridWindow" Height="180" Width="400">

        <Grid Grid.IsSharedSizeScope="True">
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Clumn0"></ColumnDefinition>
                <ColumnDefinition Width="50"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="Auto" SharedSizeGroup="Clumn0"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Row="0" Grid.Column="0">A</Button>
            <Button Grid.Row="0" Grid.Column="1">B</Button>
            <Button Grid.Row="0" Grid.Column="2">C</Button>
            <Button Grid.Row="0" Grid.Column="3">D</Button>
            <Button Grid.Row="0" Grid.Column="4">Other Text</Button>
        </Grid>

</Window>
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

if ($host.Version.Major -eq 3) {
    add-type -assembly WindowsBase,PresentationCore,PresentationFramework,System.Xml,System.Xaml
} else {
    add-type -assembly WindowsBase,PresentationCore,PresentationFramework,System.Xml
}
$form = [System.Windows.Markup.XamlReader]::Parse($xaml_win)
[void]$Form.ShowDialog()
exit
--------------------------------------------------------------

0 件のコメント:

コメントを投稿