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

アイテム選択の通知
    「SelectionChanged」イベント発生で知らせてくる。

    ちなみに、このイベントの定義場所は以下の通り

        >   (type: ListBox -s)[0].GetEvents() | ? name -EQ "SelectionChanged"

            MemberType       : Event
            Name             : SelectionChanged
            DeclaringType    : System.Windows.Controls.Primitives.Selector      <--★ここで定義
            ReflectedType    : System.Windows.Controls.ListBox
            MetadataToken    : 335544480
            Module           : PresentationFramework.dll
            Attributes       : None
AddMethod     : Void add_SelectionChanged(System.Windows.Controls.SelectionChangedEventHandler)
RemoveMethod: Void remove_SelectionChanged(System.Windows.Controls.SelectionChangedEventHandler)
            RaiseMethod      :
            EventHandlerType : System.Windows.Controls.SelectionChangedEventHandler
            IsSpecialName    : False
            IsMulticast      : True
            CustomAttributes : {[System.ComponentModel.CategoryAttribute("Behavior")]}

    補足(実際にはルーテッド・イベントとして定義)  <--後程...
        >   [System.Windows.EventManager]::GetRoutedEvents() | where-object { $_.name -eq "SelectionChanged" } | fl

            Name            : SelectionChanged  <--★こっち
            RoutingStrategy : Bubble
            HandlerType     : System.Windows.Controls.SelectionChangedEventHandler
            OwnerType       : System.Windows.Controls.Primitives.Selector

            Name            : SelectionChanged
            RoutingStrategy : Bubble
            HandlerType     : System.Windows.RoutedEventHandler
            OwnerType       : System.Windows.Controls.Primitives.TextBoxBase

実際に使ってみる

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

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="ListBox2調査" Height="220" Width="250">

    <StackPanel>
        <ListBox Name="lstbxWives" HorizontalAlignment="Left" Width="100">
            <ListBoxItem>瀬名姫</ListBoxItem>
            <ListBoxItem>濃姫</ListBoxItem>
            <ListBoxItem>ねね姫</ListBoxItem>
        </ListBox>
    </StackPanel>
</Window>
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

$code = @'
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyButton
{
    public class lstbxButtonClass
    {
        public void subscribe(ListBox l)

        {
            l.SelectionChanged += lstbxWives_SelectionChanged;
        }

        private void lstbxWives_SelectionChanged( object sender, SelectionChangedEventArgs e )
        {
            ListBox lb = sender as ListBox;
            ListBoxItem lbi = lb.SelectedItem as ListBoxItem;
            MessageBox.Show( lbi.Content.ToString()+ " が、選択されました", "通知" );
        }
    }
}
'@  # <--ブラウザによっては、コピペ時に行頭に空白出来る。空白削除する必要あり

if ($host.Version.Major -eq 3) {
    Add-Type $code -ReferencedAssemblies @("WindowsBase","PresentationCore","PresentationFramework","system.xaml")
} else {
    Add-Type $code -ReferencedAssemblies @("WindowsBase","PresentationCore","PresentationFramework")
}
$form = [System.Windows.Markup.XamlReader]::Parse($xaml_win)
$m = new-object MyButton.lstbxButtonClass
$m.subscribe($form.findname("lstbxWives"))
[void]$form.showdialog()
exit
#-------------------------------------------------------

0 件のコメント:

コメントを投稿