<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1.CustomElementBinding"
Title="Custom Element Binding Demo">
<StackPanel>
<ScrollBar Name="scroll"
Orientation="Horizontal"
Margin="24"
Maximum="100"
LargeChange="10"
SmallChange="1"
Value="{Binding ElementName=simple, Path=Number,Mode=TwoWay}" />
<src:SimpleElement Number="{Binding ElementName=scroll,Path=Value,Mode=OneWay}"/>
</StackPanel>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Globalization
Imports System.Windows
Imports System.Windows.Media
Namespace WpfApplication1.CustomElementBinding
Class SimpleElement
Inherits FrameworkElement
Public Shared NumberProperty As DependencyProperty
Shared Sub New()
NumberProperty = DependencyProperty.Register("Number", GetType(Double), GetType(SimpleElement), New FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender))
End Sub
Public Property Number() As Double
Get
Return CDbl(GetValue(NumberProperty))
End Get
Set
SetValue(NumberProperty, value)
End Set
End Property
Protected Overrides Function MeasureOverride(sizeAvailable As Size) As Size
Return New Size(200, 250)
End Function
Protected Overrides Sub OnRender(dc As DrawingContext)
dc.DrawText(New FormattedText(Number.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, New Typeface("Times New Roman"), 12, SystemColors.WindowTextBrush), New Point(0, 0))
End Sub
End Class
End Namespace