Class MainWindow
Private ActBorder As Class1
Private Function GetMyBinding(sObj As DependencyObject, sDp As DependencyProperty, strF As String) As Binding
Dim b As New Binding With {
.Source = sObj,
.Path = New PropertyPath(sDp),
.Mode = BindingMode.TwoWay,
.StringFormat = strF}
Return b
End Function
Private Sub cbXY_Checked()
If cbXY.IsChecked Then
ActBorder.SetScaleLink(True)
Else
ActBorder.SetScaleLink(False)
End If
End Sub
Private Sub MainWindow_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
ActBorder = New Class1() With {.Width = 30, .Height = 30, .Background = Brushes.Tomato}
MyCanvas.Children.Add(ActBorder)
Canvas.SetLeft(ActBorder, 100)
Canvas.SetTop(ActBorder, 50)
Dim bindX As Binding = GetMyBinding(ActBorder, Class1.MyScaleXProperty, "ScaleX = {0:0.0}")
Dim bindY As Binding = GetMyBinding(ActBorder, Class1.MyScaleYProperty, "ScaleY = {0:0.0}")
sldXscale.SetBinding(Slider.ValueProperty, bindX)
sldYscale.SetBinding(Slider.ValueProperty, bindY)
tbScaleX.SetBinding(TextBlock.TextProperty, bindX)
tbScaleY.SetBinding(TextBlock.TextProperty, bindY)
AddHandler cbXY.Click, AddressOf cbXY_Checked
AddHandler btXadd.Click, AddressOf ScaleXAdd
AddHandler btXsub.Click, AddressOf ScaleXSub
End Sub
Private Sub ScaleXAdd()
ActBorder.MyScaleX += 1
End Sub
Private Sub ScaleXSub()
ActBorder.MyScaleX -= 1
End Sub
End Class
Public Class Class1
Inherits Border
Private MyScaleTransform As ScaleTransform
Public Shared ReadOnly Property MyScaleXProperty As DependencyProperty =
DependencyProperty.Register(NameOf(MyScaleX), GetType(Double), GetType(Class1), New PropertyMetadata(1.0))
Public Property MyScaleX As Double
Get
Return GetValue(MyScaleXProperty)
End Get
Set(value As Double)
SetValue(MyScaleXProperty, value)
End Set
End Property
Public Shared ReadOnly Property MyScaleYProperty As DependencyProperty =
DependencyProperty.Register(NameOf(MyScaleY), GetType(Double), GetType(Class1), New PropertyMetadata(1.0))
Public Property MyScaleY As Double
Get
Return GetValue(MyScaleYProperty)
End Get
Set(value As Double)
SetValue(MyScaleYProperty, value)
End Set
End Property
Private Function GetMyBinding(sObj As DependencyObject, sDp As DependencyProperty) As Binding
Dim b As New Binding With {
.Source = sObj,
.Path = New PropertyPath(sDp),
.Mode = BindingMode.TwoWay}
Return b
End Function
Public Sub New()
Call Me.New(1.0, 1.0)
End Sub
Public Sub New(sx As Double, sy As Double)
MyScaleX = sx
MyScaleY = sy
MyScaleTransform = New ScaleTransform
Dim sk As New SkewTransform
Dim ro As New RotateTransform
Dim tg As New TransformGroup
With tg.Children
.Add(MyScaleTransform) : .Add(sk) : .Add(ro)
End With
Me.RenderTransform = tg
Me.RenderTransformOrigin = New Point(0.5, 0.5)
BindingOperations.SetBinding(MyScaleTransform, ScaleTransform.ScaleXProperty, GetMyBinding(Me, MyScaleXProperty))
BindingOperations.SetBinding(MyScaleTransform, ScaleTransform.ScaleYProperty, GetMyBinding(Me, MyScaleYProperty))
End Sub
Public Sub SetScaleLink(IsLink As Boolean)
If IsLink Then
BindingOperations.SetBinding(Me, MyScaleYProperty, GetMyBinding(Me, Class1.MyScaleXProperty))
Else
BindingOperations.SetBinding(Me, MyScaleYProperty, New Binding)
MyScaleY = MyScaleX
End If
End Sub
End Class