Viewbox

Viewboxは、子コントロールを拡大縮小して表示できる。

Viewbox Class (System.Windows.Controls) | Microsoft Docs
WPF4.5入門 その16 「ViewBoxコントロール」 - かずきのBlog@hatena

Viewboxと表示するButtonのサイズ
Viewboxと表示するButtonのサイズ

Viewboxと表示するButtonのサイズを決めておく。

拡大表示
拡大表示

やたらと拡大されて表示される。

Sample/wpf/Viewbox/Viewbox/src/Viewbox_ at master · bg1bgst333/Sample · GitHub

ListBoxItem

ListBoxItemは、ListBox内の選択可能な項目。

ListBoxItem Class (System.Windows.Controls) | Microsoft Docs

ListBoxItemを追加していく
ListBoxItemを追加していく

このようにListBoxの中にListBoxItemを追加していき、ListBoxItemの中に表示するテキストを書く。

こんな風に
こんな風に

こんな風に、

またはこんな感じに
またはこんな感じに

またはこんな感じに選択できる。

Sample/wpf/ListBoxItem/ListBoxItem/src/ListBoxItem_ at master · bg1bgst333/Sample · GitHub

ListBox

ListBoxは、選択可能な項目を持つリストボックスを表示する。

ListBox Class (System.Windows.Controls) | Microsoft Docs

Buttonに挟まれるような形でListBoxを配置
Buttonに挟まれるような形でListBoxを配置

Buttonに挟まれるような形でListBoxを配置。

表示だけ
表示だけ

何も項目がないので選択ができない。

Sample/wpf/ListBox/ListBox/src/ListBox_ at master · bg1bgst333/Sample · GitHub

Label

Labelコントロールは編集不可能なテキストの表示に使う。

Label Class (System.Windows.Controls) | Microsoft Docs

TextBlockとの違いは、ContentControlの派生クラスであるということ。

TextBlockはTextでLabelはContent
TextBlockはTextでLabelはContent

TextBlockはTextで、LabelはContent。

LabelはContentを持つので、

中にImageタグなどを置ける
中にImageタグなどを置ける

中にImageタグなどを置ける。

しかしなんと中に置けてしまう
しかしなんと中に置けてしまう

しかしなんとTextBlockでも中に置けてしまう。
(この理由についてはいずれ扱う。)

こうできちゃう
こうできちゃう

こうできちゃう。

Sample/wpf/Label/Label/src/Label_ at master · bg1bgst333/Sample · GitHub

Image

Imageコントロールで画像を表示する。

Image Class (System.Windows.Controls) | Microsoft Docs

f:id:BG1:20190714161020p:plain
StackPanelの下にImageタグを配置

StackPanelの下にImageタグを配置。

こんな感じ
こんな感じ

こんな感じ。

フォルダを追加
フォルダを追加

フォルダを追加。

こんな風に
こんな風に

こんな風に。

既存の項目を追加
既存の項目を追加

既存の項目を追加。

画像ファイルを追加
画像ファイルを追加

画像ファイルを追加。

こんな感じ
こんな感じ

こんな感じ。

Sourceで画像のパスをセット
Sourceで画像のパスをセット

Sourceで画像のパスをセット。

プレビューに画像が出ているので成功っぽい
プレビューに画像が出ているので成功っぽい

プレビューに画像が出ているので成功っぽい。

確かに表示できた。
確かに表示できた。

確かに表示できた。

Sample/wpf/Image/Image/src/Image_ at master · bg1bgst333/Sample · GitHub

ContentPropertyAttribute

Buttonのようなコンテンツを指定できるタグはどうやってできるのか。
また、コレクションに関するXAMLの書き方についても扱う。

XAML側
XAML

XAML側をこう書いただけでは、TestClassがないので当然こうなる。
TestClass.csに、

// 名前空間
namespace BGST
{

    // テストクラス
    public class TestClass  // TestClassの定義
    {

        // 普通の文字列プロパティ.
        private string testProperty;    // string型のフィールドtestProperty.
        public string TestProperty
        {
            // 取得
            get
            {
                return testProperty;    // testPropertyを返す.
            }
            // 設定
            set
            {
                testProperty = value;   // testPropertyに代入.
            }
        }

    }
}

TestClassを定義することで、

認識はする
認識はする

認識はする。
ただし、プロパティはタグ内の属性でしか指定できない。
さて、

// 名前空間の登録
using System.Collections.ObjectModel;   // コレクションオブジェクトモデルクラス(System.Collections.ObjectModel名前空間)

// 名前空間
namespace BGST
{

    // テストクラス
    public class TestClass  // TestClassの定義
    {

        // 普通の文字列プロパティ.
        private string testProperty;    // string型のフィールドtestProperty.
        public string TestProperty
        {
            // 取得
            get
            {
                return testProperty;    // testPropertyを返す.
            }
            // 設定
            set
            {
                testProperty = value;   // testPropertyに代入.
            }
        }

        // 自身のコレクションのプロパティ.
        public Collection<TestClass> TestCollection
        {
            get;
            set;
        }

    }

}

自身のコレクションを持つプロパティTestCollectionを定義する。
すると、

TestCollectionサブタグの下に自身のタグ
TestCollectionサブタグの下に自身のタグ

TestCollectionサブタグの下に自身のタグを入れ子出来る。

ビルドは通る
ビルドは通る

ビルドは通る。

例外
例外

しかし、例外になってしまう。

// 名前空間の登録
using System.Collections.ObjectModel;   // コレクションオブジェクトモデルクラス(System.Collections.ObjectModel名前空間)

// 名前空間
namespace BGST
{

    // テストクラス
    public class TestClass  // TestClassの定義
    {

        // コンストラクタ
        public TestClass()
        {
            // コレクションの初期化.
            TestCollection = new Collection<TestClass>();   // TestCollectionを生成しておかないといけない.
        }

        // 普通の文字列プロパティ.
        private string testProperty;    // string型のフィールドtestProperty.
        public string TestProperty
        {
            // 取得
            get
            {
                return testProperty;    // testPropertyを返す.
            }
            // 設定
            set
            {
                testProperty = value;   // testPropertyに代入.
            }
        }

        // 自身のコレクションのプロパティ.
        public Collection<TestClass> TestCollection
        {
            get;
            set;
        }

    }

}

どうもTestCollectionにインスタンスがないのが原因のようなので、コンストラクタで生成するようにした。

動いたけどわからない
動いたけどわからない

動いたけど何もわからない。
さて、TestClassCollection.csを追加し、

Collection<TestClass>を別クラスのTestClassCollectionとした。

そして、

// 名前空間の登録
using System.Collections.ObjectModel;   // コレクションオブジェクトモデルクラス(System.Collections.ObjectModel名前空間)
using System.Windows.Markup;    // マークアップ(System.Windows.Markup名前空間)

// 名前空間
namespace BGST
{

    // テストクラス
    public class TestClass  // TestClassの定義
    {

        // コンストラクタ
        public TestClass()
        {
            // コレクションの初期化.
            TestCollection = new TestClassCollection(); // TestClassCollectionを生成.
        }

        // 普通の文字列プロパティ.
        private string testProperty;    // string型のフィールドtestProperty.
        public string TestProperty
        {
            // 取得
            get
            {
                return testProperty;    // testPropertyを返す.
            }
            // 設定
            set
            {
                testProperty = value;   // testPropertyに代入.
            }
        }

        // 自身のコレクションのプロパティ.
        public TestClassCollection TestCollection
        {
            get;
            set;
        }

    }

}

として、

TestClassCollectionタグ
TestClassCollectionタグ

サブタグの下にコレクション、コレクションの下にアイテムという形にできた。

これでも動いた
これでも動いた

これでも動いた。

コレクションクラスは省略できる
コレクションクラスは省略できる

しかし、実はコレクションクラスはなくても、

動く
動く

動く。
これがコレクション構文。

WPF4.5入門 その9 「コレクション構文」 - かずきのBlog@hatena

さらに、

ContentProperty属性で指定したプロパティは、ButtonのContentのように中に直接書くことができる。

ContentPropertyAttribute Class (System.Windows.Markup) | Microsoft Docs

なので、

サブタグも省略
サブタグも省略

サブタグも省略できる。
これがコンテンツ構文。

WPF4.5入門 その10 「コンテンツ構文」 - かずきのBlog@hatena

これでTestClassタグを入れ子できることがわかる。

見た目は変わらない
見た目は変わらない

見た目は変わらないようだが・・・。

Sample/wpf/ContentPropertyAttribute/ContentPropertyAttribute/src/ContentPropertyAttribute_ at master · bg1bgst333/Sample · GitHub

Collection<T>

Collection<T>は、ジェネリクスなコレクションの基本クラス。

https://docs.microsoft.com/ja-jp/dotnet/api/system.collections.objectmodel.collection-1?view=netframework-4.8

<T>のないバージョンはない。
List<T>を先にやってしまったが、本来こちらを先にやるべきだった・・・。
List<T>には一括追加や検索機能があるが、Collection<T>にはそれがないということぐらいか・・・。

まあ、でも、今回は普通にアクセス。

i = 1
i = 2
i = 3
i = 4
i = 5
続行するには何かキーを押してください . . .

普通にアクセスできる。

Sample/dotnet/Collection_T/Collection_T/src/Collection_T_ at master · bg1bgst333/Sample · GitHub