Here is an example of how...
<Window x:Class="WpfCalc.MainWindow"
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:WpfCalc"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:CalcViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<Label x:Name="lblItem" Content="Materials" Grid.Row="0" Grid.Column="0" VerticalAlignment="Top"></Label>
<Label x:Name="lblPrice" Content="Price" Grid.Row="0" Grid.Column="1" VerticalAlignment="Top"></Label>
<Label x:Name="lblOil" Content="Oil" Grid.Row="1" Grid.Column="0" VerticalAlignment="Top"></Label>
<TextBox x:Name="txtOil" Grid.Row="1" Grid.Column="1" VerticalAlignment="Top"
Text="{Binding Oil, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Label x:Name="lblRice" Content="Rice" Grid.Row="2" Grid.Column="0" VerticalAlignment="Top"></Label>
<TextBox x:Name="txtRice" Grid.Row="2" Grid.Column="1" VerticalAlignment="Top"
Text="{Binding Rice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Label x:Name="lblCoconut" Content="Cocunut" Grid.Row="3" Grid.Column="0" VerticalAlignment="Top"></Label>
<TextBox x:Name="txtCoconut" Grid.Row="3" Grid.Column="1" VerticalAlignment="Top"
Text="{Binding Coconut, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>
and the ViewModel:
using System.ComponentModel;
namespace WpfCalc
{
public class CalcViewModel : INotifyPropertyChanged
{
public CalcViewModel()
{
Oil = 123.456F;
Rice = 222.222F;
Coconut = 333.333F;
}
private float oil;
public float Oil
{
get { return oil; }
set
{
if (oil != value)
{
oil = value;
NotifyPropertyChanged(nameof(Oil));
}
}
}
private float rice;
public float Rice
{
get { return rice; }
set
{
if (rice != value)
{
rice = value;
NotifyPropertyChanged(nameof(Rice));
}
}
}
private float coconut;
public float Coconut
{
get { return coconut; }
set
{
if (coconut != value)
{
coconut = value;
NotifyPropertyChanged(nameof(Coconut));
}
}
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
Also, check out
MVVM Light Toolkit - Nuget[
^]