I'm building a .NET MAUI app using the CommunityToolkit.Mvvm [ObservableProperty] attribute for data binding.
Example:
[ObservableProperty]
public decimal _count;
Setting the value:
decimal count = 2;
Item.Count = count;
If Count is a normal property, the UI shows 2 as expected.
But when using [ObservableProperty], the UI shows 2.000.
I need the number of decimal places to be fully dynamic:
- If it's an integer value → show 2
- If it's 2.5 → show 2.5
- If it's 2.1234567890 → show exactly that (up to 10 decimals in my case)
I cannot hardcode a format like "0.##" because the number of decimal places changes depending on other data in the object.
Question: why is [ObservableProperty] changing the formatting in the UI, and how can I keep the property observable while showing a completely dynamic number of decimal places?
Notes:
- Item is part of my view model.
- I'm using .NET MAUI and CommunityToolkit.Mvvm.
- Formatting should reflect the actual decimal value — no forced trailing zeros.
- The formatting issue only happens with
[ObservableProperty]orINotifyPropertyChanged
Edit:
Here is the Code for my UI:
<CollectionView Grid.Row="1"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
SelectionMode="Single"
EmptyView="No Data"
ItemSizingStrategy="MeasureFirstItem"
x:Name="List">
<CollectionView.ItemTemplate>
<DataTemplate>
<VerticalStackLayout>
<Grid ColumnDefinitions=".5*, .25*, .25*"
Padding="8,8,8,8"
RowSpacing="8" HeightRequest="40">
<Label Text="{Binding Name}" Grid.Column="0" FontSize="12" VerticalOptions="Center" HorizontalOptions="Start"/>
<Label Text="{Binding Name2}" Grid.Column="1" FontSize="12" VerticalOptions="Center" HorizontalOptions="End"/>
<Label Text="{Binding Count}" Grid.Column="2" FontSize="12" VerticalOptions="Center" HorizontalOptions="End"/>
</Grid>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
ObservabelPropertyAttribute)