Hi @Heinz Deubler ,
Thanks for reaching out.
I created a project locally to reproduce your setup. Below is the full working example you can use.
ViewModels/TemperatureViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using System.Globalization;
namespace TempConverter.ViewModels
{
public partial class TemperatureViewModel : ObservableObject
{
[ObservableProperty]
private string _fahrenheit;
[ObservableProperty]
private string _celsius;
private bool _isProgrammaticUpdate = false;
partial void OnFahrenheitChanged(string value)
{
if (_isProgrammaticUpdate) return;
_isProgrammaticUpdate = true;
if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var f))
{
var c = (f - 32.0) / 1.8;
Celsius = c.ToString("0.##", CultureInfo.InvariantCulture);
}
else
{
Celsius = string.Empty;
}
_isProgrammaticUpdate = false;
}
partial void OnCelsiusChanged(string value)
{
if (_isProgrammaticUpdate) return;
_isProgrammaticUpdate = true;
if (double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var c))
{
var f = (c * 1.8) + 32.0;
Fahrenheit = f.ToString("0.##", CultureInfo.InvariantCulture);
}
else
{
Fahrenheit = string.Empty;
}
_isProgrammaticUpdate = false;
}
}
}
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TempConverter.MainPage">
<VerticalStackLayout Margin="10,30,20,10">
<Grid Background="Azure" Padding="10">
<!-- Column Definitions -->
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="1200" />
</Grid.ColumnDefinitions>
<!-- Row Definitions -->
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<!-- Celsius Label -->
<Label Grid.Row="1"
Grid.Column="1"
Text="Celsius"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start" />
<!-- Celsius Entry -->
<Entry Grid.Row="2"
Grid.Column="1"
Text="{Binding Celsius, Mode=TwoWay}"
Keyboard="Numeric"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start"
Placeholder="Celsius" />
<!-- Fahrenheit Label -->
<Label Grid.Row="3"
Grid.Column="1"
Text="Fahrenheit"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start" />
<!-- Fahrenheit Entry -->
<Entry Grid.Row="4"
Grid.Column="1"
Text="{Binding Fahrenheit, Mode=TwoWay}"
Keyboard="Numeric"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start"
Placeholder="Fahrenheit" />
</Grid>
</VerticalStackLayout>
</ContentPage>
MainPage.xaml.cs
using TempConverter.ViewModels;
namespace TempConverter
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new TemperatureViewModel();
}
}
}
Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.