午後わてんのブログ

ベランダ菜園とWindows用アプリ作成(WPFとC#)

WPF、AnglePropertyと作成した依存プロパティをバインディング

 
作成した依存プロパティと
RenderTransformの中のRotateTransformのAnglePropertyをBindingしておいて
スライダーやテキストボックスともバインディング
 
イメージ 1
今回のアプリのダウンロード先
デザイン画面
ヤフーブログのかんたんモードでXAMLを書くと投稿エラーになるから画像で
イメージ 2
 
 
VBコード
今回はMainWindowの他にBorderクラスを継承したClass1の2つ
Class MainWindow
    Private ActBorder As Class1

    '今の回転角度に+10する
    Private Sub AddAngle10()
        ActBorder.MyAngle += 10
    End Sub
    Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
        AddHandler btnAngle.Click, AddressOf AddAngle10
        'Class1作成、回転角度の初期値に20を設定
        Dim MyBorder = New Class1(20) With {.Width = 100, .Height = 50, .Background = Brushes.Red}
        MyCanvas.Children.Add(MyBorder)
        Canvas.SetLeft(MyBorder, 20)
        Canvas.SetTop(MyBorder, 30)
        ActBorder = MyBorder

        'バインディングソース作成
        Dim b As New Binding With {
    .Source = MyBorder,
    .Path = New PropertyPath(Class1.MyAngleProperty),
    .Mode = BindingMode.TwoWay,
    .StringFormat = "Angle = {0:0}"}
        'スライダーとテキストブロックそれぞれにバインディング
        sldAngle.SetBinding(Slider.ValueProperty, b)
        tbAngle.SetBinding(TextBlock.TextProperty, b)
    End Sub
End Class
 
Public Class Class1
    Inherits Border

    '依存プロパティ
    Public Shared ReadOnly Property MyAngleProperty As DependencyProperty =
        DependencyProperty.Register(NameOf(MyAngle), GetType(Double), GetType(Class1))
    Public Property MyAngle As Double
        Get
            Return GetValue(MyAngleProperty)
        End Get
        Set(value As Double)
            SetValue(MyAngleProperty, value)
        End Set
    End Property
    'コンストラクタ、回転角度を初期値に指定できる
    Public Sub New(Optional angle As Double = 0)
        '各種TransformをグループにしてRenderTransformに指定
        Dim sc As New ScaleTransform '拡縮
        Dim sk As New SkewTransform 'ひし形
        Dim ro As New RotateTransform() '回転
        Dim tg As New TransformGroup
        With tg.Children 'transformグループ作成
            .Add(sc)
            .Add(sk)
            .Add(ro)
        End With
        Me.RenderTransform = tg '指定
        Me.RenderTransformOrigin = New Point(0.5, 0.5) '変形の基準点は中心
        MyAngle = angle '回転角度の初期値設定

        'バインディング
        '作成した依存プロパティのMyAnglePropertyをソースにして
        'RenderTransformの中のRotateTransformのAnglePropertyをターゲットにする場合
        Dim b As New Binding With {
            .Source = Me,
            .Path = New PropertyPath(MyAngleProperty),
            .Mode = BindingMode.TwoWay}
        BindingOperations.SetBinding(ro, RotateTransform.AngleProperty, b)

        '↑↓ソースとターゲットが入れ替わっているだけ

        'Dim b As New Binding With {
        '    .Source = ro,
        '    .Path = New PropertyPath(RotateTransform.AngleProperty),
        '    .Mode = BindingMode.TwoWay}
        'Me.SetBinding(MyAngleProperty, b)

    End Sub
End Class
 
 
今回の方法は少し回りくどいかなあって気もするけどどうかなあ
回転角度のAnglePropertyはRenderTransformの中のTransformGroupのChildrenのどれかのRotateTransformの中っていう奥の方にあるから角度指定するときめんどくさかったけど今回のならMyAngleだけだからラク
Pixtack紫陽花2ndのときはスライダーのChangeValueイベント発生時に普通のプロパティを変更する方法だったかなあ、依存プロパティを使いたかったけど難しくてできなかった、今回それっぽく使えるようになったのでメモ
わかんないのが、DependencyProperty.Registerの4番目の引数を付けて初期値を設定したほうがいいのかなあ
 
初期値にDouble型の0を指定(普通に0を指定したら型が違うってエラーになったw)
Public Shared ReadOnly Property MyAngleProperty As DependencyProperty =
DependencyProperty.Register(NameOf(MyAngle),
GetType(Double),
GetType(Class1),
New PropertyMetadata(CDbl(0)))
 
 
 
NameOFが便利
イメージ 3
6行目のNameOFってのはプロパティとかを指定するとStringに変換して返すだけなんだけど便利
a: NameOF(MyAngle)
b: "MyAngle"
a,bどちらでも同じ結果だしbのほうが短くて済むんだけど
もしMyAngleってプロパティ名を別の名前に変えたくなったとき
イメージ 4
Myを取ってAngleに変更
イメージ 5
NameOFの引数の方も自動で変わる
もし文字列指定の"MyAngle"だったら自動では変わらないので手動で変えることになると思っていたら
イメージ 6
名前の変更時に出てくるこの画面で文字列を含めるにチェックを入れて変更すればできるみたい
でもNameOFのほうなら打ち間違えもないし確実だと思う
 
 
 
 
参照したところ
WPF】自分で定義したプロパティにバインディングする – ザワプロ!

zawapro.com



WPF4.5入門 その42 「WPFのプロパティシステム」 - かずきのBlog@hatena

blog.okazuki.jp



WPF 依存プロパティの作り方 - Qiita

qiita.com



[VB 14.0 新機能] NameOf演算子 | HIRO's.NET Blog

blog.hiros-dot.net

ありがとうございます!
 
 
 
コード全部
 
次回は翌日
WPF、ScaleTransformと作成した依存プロパティをBinding ( ソフトウェア ) - 午後わてんのブログ - Yahoo!ブログ
https://blogs.yahoo.co.jp/gogowaten/14981395.html
 
 
 
関連記事
1年前
2016/5/26
WPFVB.NET、TransformGroupのChildrenにAddする順番で結果が変わる ( ソフトウェア ) - 午後わてんのブログ - Yahoo!ブログ
https://blogs.yahoo.co.jp/gogowaten/14169953.html
 
 
これも1年前
2016/4/29
WPFVB.NET、Bindingしたままコントロールを直接変形、TransformGroupの中のRotateTransform ( ソフトウェア ) - 午後わてんのブログ - Yahoo!ブログ
https://blogs.yahoo.co.jp/gogowaten/14098125.html