2013年1月4日金曜日

コンテンツ(CheckBox, RadioButton)


ContentControlクラス    単一エレメントを保持することができるContentと呼ばれるプロパティが含まれている
                                       (自分の子供として単一エレメントを保持する)
       クラス定義は、

                Module Name: PresentationFramework.dll

          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

         単一エレメントを保持する「Content」プロパティは

                >   [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
ContentProperty

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

        IsPublic IsSerial Name                           BaseType
        -------- -------- ----                                   --------
        True     False    Window                          System.Windows.Controls.ContentControl
        True     False    Label                             System.Windows.Controls.ContentControl
        True     False    ScrollViewer                    System.Windows.Controls.ContentControl
        True     False    ButtonBase                     System.Windows.Controls.ContentControl
        True     False    ListBoxItem                     System.Windows.Controls.ContentControl
        True     False    HeaderedContentControl System.Windows.Controls.ContentControl
        True     False    Frame                            System.Windows.Controls.ContentControl
        True     False    GroupItem                       System.Windows.Controls.ContentControl
        True     False    StatusBarItem                 System.Windows.Controls.ContentControl
        True     False    ToolTip                           System.Windows.Controls.ContentControl
        True     False    UserControl                     System.Windows.Controls.ContentControl


CheckBoxとRaidoBoxの調査

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

                Module Name: PresentationFramework.dll

           System.Windows.Controls.CheckBox
             System.Windows.Controls.Primitives.ToggleButton
               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


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


                Module Name: PresentationFramework.dll

           System.Windows.Controls.RadioButton
             System.Windows.Controls.Primitives.ToggleButton
               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


実際に使ってみる

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

 自動グループ化(RadioButton) ← (標準)同じ親を持つ兄弟ボタンを同一グループ化

---------------------------------------------
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"
        Width="400" Height="380">

    <StackPanel>
        <CheckBox Name="cb1" Margin="5,10,0,0">Option 1</CheckBox>
        <CheckBox Name="cb2" Margin="5,0,0,0">Option 2</CheckBox>
        <RadioButton Name="rb1" Margin="5,10,0,0">One of Three</RadioButton>
        <RadioButton Name="rb2" Margin="5,0,0,0">Two of Three</RadioButton>
        <RadioButton Name="rb3" Margin="5,0,0,0">Three of Three</RadioButton>
    </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
--------------------------------------------------------------
★CheckBox、RadioButtonのそれぞれの「Content」プロパティは以下のようになっている
> 0..4 | % { ($Form.Content.Children)[$_].Content }
                Option 1
                Option 2
                One of Three
                Two of Three
                Three of Three


グループ化の指定
--------------------------------------------------------------
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"
        Width="400" Height="380">

    <StackPanel>
        <RadioButton Margin="5,0,5,0">Default 1</RadioButton>
        <RadioButton Margin="5,0">Default 2</RadioButton>
        <RadioButton GroupName="Group1" Margin="5,7,5,0">Group1-1</RadioButton>
        <RadioButton GroupName="Group1" Margin="5,0">Group1-2</RadioButton>
        <RadioButton GroupName="Group2" Margin="5,7,5,0">Group2-1</RadioButton>
        <RadioButton GroupName="Group2" Margin="5,0">Group2-2</RadioButton>
    </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
--------------------------------------------------------------
★RadioButtonのそれぞれの「Content」プロパティは以下ようになっている
> 0..5 | % { ($Form.Content.Children)[$_].Content }
                Default 1
                Default 2
                Group1-1
                Group1-2
                Group2-1
                Group2-2

0 件のコメント:

コメントを投稿