ラベル WPFレイアウト の投稿を表示しています。 すべての投稿を表示
ラベル WPFレイアウト の投稿を表示しています。 すべての投稿を表示

2013年1月3日木曜日

レイアウト(UniformGrid)


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的


UniformGrid
    Gridの超シンプル版みたいなやつ(均等配置、順番固定、単一コンテンツ)
    まあ十分な時もあるかも


実際に確認してみる


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


---------------------------------------------
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="150" Width="300">

    <UniformGrid Rows="2" Columns="2">
        <Button>Button 1</Button>
        <Button>Button 2</Button>
        <Button>Button 3</Button>
        <Button>Button 4</Button>
    </UniformGrid>

</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
--------------------------------------------------------------

レイアウト(Canvas:3次元)


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的


Canvas  Z-Order(3次元)の表現
    Canvas.ZIndex  アタッチドプロパティにより指定できる
        ①ZIndex値の大きいほうが前
        ②同じ値の時は後に定義した方が前
        ③ZIndex省略時の値はゼロ


実際に確認してみる

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

---------------------------------------------
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="160" Width="160">

    <Canvas>
        <Button Canvas.Top="32" Canvas.Left="30" Canvas.ZIndex="5">Top Left</Button>
        <Button Canvas.Top="20" Canvas.Right="30">Top Right</Button>
    </Canvas>

</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
--------------------------------------------------------------

レイアウト(Canvas:基本)


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的


Canvas基本形

Canvasエレメントの指定の座標から絶対値で内部に置くコントロールを指定する
    ・Canvas.Left    ="nnn"
    ・Canvas.Right   ="mmm"
    ・Canvas.Top     ="xxx"
    ・Canvas.Bottom="yyy"


実際に確認してみる


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


---------------------------------------------
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">

<Canvas>
<Button Canvas.Top="20" Canvas.Left="30">Top Left</Button>
<Button Canvas.Top="20" Canvas.Right="30">Top Right</Button>
<Button Canvas.Bottom="20" Canvas.Left="30">Bottom Left</Button>
<Button Canvas.Bottom="20" Canvas.Right="30">Bottom Right</Button>
</Canvas>

</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
--------------------------------------------------------------

レイアウト(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
--------------------------------------------------------------

レイアウト(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的


隙間バー(GridSplitterクラス)

    各Cell間の間を少し開けたい時に使う、専用の特殊なコントローラ。
    Gridから見たら、普通にCell内に入れるコントローラと思っている。
            ↑↑↑(ただし)
    このコントローラをドラッグするとCellサイズが同的に変化する(^^;)

ちなみに、「gridsplitter」クラスの定義は以下の通り


        Module Name: PresentationFramework.dll

   System.Windows.Controls.GridSplitter
     System.Windows.Controls.Primitives.Thumb
       System.Windows.Controls.Control
         System.Windows.FrameworkElement
           System.Windows.UIElement
             System.Windows.Media.Visual
               System.Windows.DependencyObject
                 System.Windows.Threading.DispatcherObject
                   System.Object


実際に確認してみる


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



---------------------------------------------
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.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Button Grid.Row="0" Grid.Column="0">Button 1</Button>
            <Button Grid.Row="1" Grid.Column="0">Button 2</Button>
            <GridSplitter Grid.Row="0" Grid.Column="1"
                          Width="3"                  
                          Grid.RowSpan="2"            
                          Background="DarkGray"      
                          HorizontalAlignment="Center"
                          VerticalAlignment="Stretch">
            </GridSplitter>
            <Button Grid.Row="0" Grid.Column="2">Button 3</Button>
            <Button Grid.Row="1" Grid.Column="2">Button 4</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
--------------------------------------------------------------

レイアウト(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的


複数Cell間でのサイズ保持

指定された複数のCell間で同じ値(またはその倍数値)を保ってサイズされる

  • Cell(0,0) は、高さ="指定なし",幅="50"       幅="50"に固定されたまま
  • Cell(0,1) は、高さ="指定なし",幅="Auto"     で幅のみコンテンツ幅に固定(高さはStretch)
  • Cell(0,2) は、高さ="指定なし",幅="*"        残りの幅を1/4して*1の幅に調整
  • Cell(0,3) は、高さ="指定なし",幅="3*"       残りの幅を1/4して*3の幅に調整


実際に確認してみる


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


---------------------------------------------
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="250">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="3*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Row="0" Grid.Column="0">Button 1</Button>
            <Button Grid.Row="0" Grid.Column="1">Button 2</Button>
            <Button Grid.Row="0" Grid.Column="2">Button 3</Button>
            <Button Grid.Row="0" Grid.Column="3">Button 4</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
--------------------------------------------------------------

レイアウト(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的


自動サイズ

Cell内のコンテンツのサイズに自動調整  ← Cell内のコンテンツに変化なければ、そのサイズで固定されているように見える

  • Cell(0,0) は、高さ="Auto"    ,幅="Auto"     でコンテンツのサイズに固定されたまま
  • Cell(1,1) は、高さ="指定なし",幅="Auto"     で幅のみコンテンツ幅に固定(高さはStretch)
  • Cell(2,2) は、高さ="指定なし",幅="指定なし" Gird全体のサイズに合わせてStretch


実際に確認してみる


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


---------------------------------------------
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="250">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition Width="Auto"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Row="0" Grid.Column="0">Button 1</Button>
            <Button Grid.Row="1" Grid.Column="1">Button 2</Button>
            <Button Grid.Row="2" Grid.Column="2">Button 3</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
--------------------------------------------------------------

レイアウト(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(行Cellと列Cellのサイズ)

    ①Grid全体のサイズは、その上位のコンテンツに従う
    ②Grid内部のCellのサイズを決定するのは以下の3方式
        ・均等サイズ    Grid全体のサイズから均等割り【規定値】
        ・絶対サイズ    サイズを手動で指定
        ・自動サイズ    Cell内のコンテンツサイズに自動調整
        ・均等サイズ    複数Cell間で同じ値(または指定あればその倍数値)

Cell絶対サイズ

    GirdのDefinitionsプロパティで指定する。以下の例では、
        Cell(0,0) は、高さ="60"      ,幅="35"       で固定されたまま
        Cell(1,1) は、高さ="指定なし",幅="150"      で幅のみ固定(高さはStretch)
        Cell(2,2) は、高さ="指定なし",幅="指定なし" Gird全体のサイズに合わせてStretch


実際に確認してみる


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


---------------------------------------------
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="250">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="60"></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="35"></ColumnDefinition>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Row="0" Grid.Column="0">Button 1</Button>
            <Button Grid.Row="1" Grid.Column="1">Button 2</Button>
            <Button Grid.Row="2" Grid.Column="2">Button 3</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
--------------------------------------------------------------

レイアウト(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(基本形)   <-- Cell内のコントロールは、GridのCellサイズにストレッチされる

  • HorizontalAlignment     : Left, Center, Right, or Stretch【規定値】
  • VerticalAlignment       : Top, Center, Bottom, or Stretch【規定値】


実際に確認してみる


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


---------------------------------------------
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="250">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Button Grid.Row="0" Grid.Column="0">Button 1</Button>
            <Button Grid.Row="1" Grid.Column="1">Button 2</Button>
            <Button Grid.Row="2" Grid.Column="2">Button 3</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
--------------------------------------------------------------

レイアウト(DockPanel)


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的

上記のうち「DockPanel」について

  • ディフォルトは、左から右 &  最後のエレメントで残り全体を埋めるように頑張る
  • ボタン③が残りの全体をうえめる例(埋めるように頑張らないは、"LastChildFill="False" )

実際に確認してみる


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



---------------------------------------------
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="MainWindow" Height="180" Width="250">

    <DockPanel LastChildFill="False">
    <!--<DockPanel>-->
        <Button DockPanel.Dock="Top">Btn 1</Button>
        <Button DockPanel.Dock="Right">Btn 2</Button>
        <Button DockPanel.Dock="Bottom">Btn 3</Button>
        <Button DockPanel.Dock="Left">Btn 4</Button>
        <Button DockPanel.Dock="Top">Btn 5</Button>
    </DockPanel >

</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
--------------------------------------------------------------

レイアウト(WrapPanel)


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的

上記のうち「WrapPanel」について

    折り返し配列は以下の4つのパターン
    Orientation="Horizontal" FlowDirection="LeftToRight"      → ↓ 左上から開始、左から右、左下への折り返し(標準)
    Orientation="Horizontal" FlowDirection="RightToLeft"      ← ↓ 右上から開始、右から左、右下への折り返し
    Orientation="Vertical"   FlowDirection="LeftToRight"      ↓ → 左上から開始、上から下、右上への折り返し
    Orientation="Vertical"   FlowDirection="RightToLeft"      ↓ ← 右上から開始、上から下、左上への折り返し


ItemHight, ItemWidth プロパティ

    配下のアイテムのサイズを強制的に指定する(指定ないときは最大までストレッチ)



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

---------------------------------------------
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="MainWindow" Height="180" Width="250">

        <WrapPanel ItemHeight="90">
                <Button>Button 1</Button>
                <Button VerticalAlignment="Top">Button 2</Button>
                <Button VerticalAlignment="Center">Button 3</Button>
                <Button VerticalAlignment="Bottom">Button 4</Button>
                <Button VerticalAlignment="Stretch">Button 5</Button>
                <!--<Button>Button 5</Button> 上記と同じ-->
        </WrapPanel>
</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
--------------------------------------------------------------

レイアウト(Panel)


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的

上記のうち「StackPanel」について
  • 下から上にはできない
  • 境界を越えて配列出来る(超えたところは見えない)


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

        <!--①<StackPanel>-->
        <!--②<StackPanel Orientation="Horizontal">-->
        <!--③<StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">-->
        <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft">
             <Button Padding="5,10">ボタン1</Button>
             <Button Padding="5,10">ボタン2</Button>
             <Button Padding="5,10">ボタン3</Button>
        </StackPanel>
</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
--------------------------------------------------------------

レイアウト(PaddingとMargin)


Padding エレメントの中の要素との間の隙間
Margin エレメントの外側の隙間

定義クラスは?

        Module Name: PresentationFramework.dll

  System.Windows.Controls.Button
    System.Windows.Controls.Primitives.ButtonBase
      System.Windows.Controls.ContentControl
        System.Windows.Controls.Control <--これ(Paddingプロパティ定義)
          System.Windows.FrameworkElement <--これ(Marginプロパティ定義)
            System.Windows.UIElement
              System.Windows.Media.Visual
                System.Windows.DependencyObject
                  System.Windows.Threading.DispatcherObject
                    System.Object
↓↓↓(確認してみる)

Paddingについて

         System.Type : System.Windows.Controls.Control

[System.Windows.Controls.Control]::System.Windows.Thickness get_Padding()
[System.Windows.Controls.Control]::Void set_Padding(System.Windows.Thickness)
[System.Windows.Controls.Control]::System.Windows.Thickness Padding
[System.Windows.Controls.Control]::System.Windows.DependencyProperty PaddingProperty  ←後程...

Marginについて

     System.Type : System.Windows.FrameworkElement

[System.Windows.FrameworkElement]::System.Windows.Thickness get_Margin()
[System.Windows.FrameworkElement]::Void set_Margin(System.Windows.Thickness)
[System.Windows.FrameworkElement]::System.Windows.Thickness Margin
[System.Windows.FrameworkElement]::System.Windows.DependencyProperty MarginProperty  ←後程...

実際に確認してみる


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



---------------------------------------------

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="MainWindow" Height="180" Width="250">

<WrapPanel>
<Button Padding="0,5,10,15" Margin="10">ボタン1</Button>
<Button Padding="5,10" Margin="10">ボタン2</Button>
</WrapPanel>

</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)
# $b = $form.content.children
# $thickness1 = new-object System.Windows.Thickness 10,10,10,10
# $thickness2 = new-object System.Windows.Thickness 10,10,10,10
# 0..($b.count-1) | % { $b[$_].set_Padding($thickness1) }
# 0..($b.count-1) | % { $b[$_].set_Margin($thickness2) }
[void]$Form.ShowDialog()
exit
--------------------------------------------------------------

レイアウト(Visibility)


3つモードがある

     System.Type : System.Windows.Visibility

    [System.Windows.Visibility]::System.Windows.Visibility Visible        <--ちゃんと計算して可視化
    [System.Windows.Visibility]::System.Windows.Visibility Hidden        <--ちゃんと計算するが不可視
    [System.Windows.Visibility]::System.Windows.Visibility Collapsed    <--レイアウト計算もしない


ちなみに「Visivility」は、どのクラスで定義するかというと(Buttonで)

        Module Name: PresentationFramework.dll

        System.Windows.Controls.Button
           System.Windows.Controls.Primitives.ButtonBase
             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

                    ↓↓↓(確認してみる)

         System.Type : System.Windows.UIElement

        [System.Windows.UIElement]::System.Windows.Visibility get_Visibility()
        [System.Windows.UIElement]::Void set_Visibility(System.Windows.Visibility)
        [System.Windows.UIElement]::System.Windows.Visibility Visibility


実際に試してみる


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


---------------------------------------------

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="MainWindow" Height="125" Width="80">

        <StackPanel>
            <Button Visibility="Visible">Visible</Button>          <!-- ○ちゃんと見えてる -->
            <Button Visibility="Hidden">Hidden</Button>         <!-- ●存在するが見えない -->
            <Button Visibility="Collapsed">Collapsed</Button> <!-- そもそもレイアウトされない -->
            <Button>無指定</Button>                                  <!-- ○最後は、ちゃんと見えてる -->
        </StackPanel>

</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
--------------------------------------------------------------

レイアウト(コンテンツ配列)

コンテンツの管理クラスは?  Button例
        Module Name: PresentationFramework.dll

    System.Windows.Controls.Button
       System.Windows.Controls.Primitives.ButtonBase
         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

「ContentControl」プロパティで整列に関係するもの <-- 「ContentAlignment」キーワード

    System.Type : System.Windows.Controls.ContentControl

[System.Windows.Controls.Control]::System.Windows.HorizontalAlignment HorizontalContentAlignment
[System.Windows.Controls.Control]::System.Windows.VerticalAlignment VerticalContentAlignment


①水平整列の設定値レパートリ調査

         System.Type : System.Windows.orizontalAlignment

[System.Windows.HorizontalAlignment]::System.Windows.HorizontalAlignment Left
[System.Windows.HorizontalAlignment]::System.Windows.HorizontalAlignment Center
[System.Windows.HorizontalAlignment]::System.Windows.HorizontalAlignment Right
[System.Windows.HorizontalAlignment]::System.Windows.HorizontalAlignment Stretch

②垂直整列の設定値レパートリ調査

System.Type : System.Windows.VerticalAlignment

[System.Windows.VerticalAlignment]::System.Windows.VerticalAlignment Top
[System.Windows.VerticalAlignment]::System.Windows.VerticalAlignment Center
[System.Windows.VerticalAlignment]::System.Windows.VerticalAlignment Bottom
[System.Windows.VerticalAlignment]::System.Windows.VerticalAlignment Stretch


実行してみる


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




---------------------------------------------
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="MainWindow" Height="325" Width="280">

    <Canvas>
        <StackPanel Canvas.Left="12" Canvas.Top="12" Height="100" Width="109">
            <Button HorizontalContentAlignment="Left">左</Button>
            <Button HorizontalContentAlignment="Center">中央</Button>
            <Button HorizontalContentAlignment="Right">右</Button>
            <Button HorizontalContentAlignment="Stretch">拡張</Button>
        </StackPanel>

        <StackPanel Orientation="Horizontal" Height="66" Width="104" Canvas.Left="43" Canvas.Top="173">
            <Button VerticalContentAlignment="Top">上</Button>
            <Button VerticalContentAlignment="Center">中央</Button>
            <Button VerticalContentAlignment="Bottom">底</Button>
            <Button VerticalContentAlignment="Stretch">拡張</Button>
        </StackPanel>
    </Canvas>

</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
--------------------------------------------------------------

レイアウト(幅と高さ)


WPFコントロールのサイズに関する重要なプロパティ

・Width、Height  固定値
・MinWidth, MaxWidth, MinHeight, MaxHeight 変動値


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

(画像ファイル:"c:\temp\work\flower.jpg"を利用)
--------------------------------------------------------------
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="MainWindow" Height="300" Width="400">

    <StackPanel>
        <Button MinWidth="125" MaxWidth="200">Short Button String</Button>
        <Image Width="250" Source="c:\temp\work\Flower.jpg"></Image>
        <Button>Long Button String</Button>
    </StackPanel>

</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
--------------------------------------------------------------