Hi Roger, don't forget:
- set DataContext or Source
- Notify property changed
- check input for invalid decimal numbers
Try following simple demo.
XAML:
<Window x:Class="Window014"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="Window014" Height="450" Width="800" Loaded="Window_Loaded">
<StackPanel>
<TextBox x:Name="txt1" Margin="5" />
<TextBox x:Name="txt2" Margin="5"/>
<TextBox Text="{Binding Diff}" Margin="5"/>
</StackPanel>
</Window>
CodeBehind:
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Public Class Window014
Implements INotifyPropertyChanged
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Me.DataContext = Me
End Sub
Private Sub txt_TextChanged(sender As Object, e As TextChangedEventArgs) Handles txt1.TextChanged, txt2.TextChanged
Dim dec1 As Decimal = 0
Decimal.TryParse(txt1.Text, dec1)
Dim dec2 As Decimal = 0
Decimal.TryParse(txt2.Text, dec2)
Diff = dec1 - dec2
OnPropChanged(NameOf(Diff))
End Sub
Public Property Diff As Decimal
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub OnPropChanged(<CallerMemberName> Optional propName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
End Sub
End Class