According to your code, I updated and tested the following code, you can refer to the code below.
The problem is that you got the data but didn't successfully assign it to the property in the ViewModel.
So,you could add property notifications for properties in the ViewModel.
<StackPanel >
<TextBlock Background="AliceBlue" Width="300" Height="50" Margin="20,0,0,0" VerticalAlignment="Center" Text="{Binding DateOfBirth,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Background="AliceBlue" Width="300" Height="50" Margin="20,0,0,0" VerticalAlignment="Center" Text="{Binding UserId,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged }" />
<TextBlock Background="AliceBlue" Width="300" Height="50" Margin="20,0,0,0" VerticalAlignment="Center" Text="{Binding City,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Background="AliceBlue" Width="300" Height="50" Margin="20,0,0,0" VerticalAlignment="Center" Text="{Binding Email,Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" />
</StackPanel>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string UserId = "DqPTMCpLNfeZuYBfixrIHyxmDZB2";
DataContext = new ViewModel(UserId);
}
}
public class ViewModel : ObservableObject
{
public ViewModel() { }
public ObservableCollection<FirebaseObject<LocalUser>>? UserData { get; set; }
public bool IsReadOnly { get; set; }
public string? BtonContent { get; set; }
private string email;
public string Email
{
get => email!;
set
{
email = value;
OnPropertyChanged();
}
}
private DateTime dateOfBirth;
public DateTime DateOfBirth
{
get => dateOfBirth!;
set
{
dateOfBirth = value;
OnPropertyChanged();
}
}
public string UserId { get; set; }
public ViewModel(string userId)
{
UserId = userId;
LoadDataFromFirebase();
// BtonContent = Lang.Edit;
var age = GetAge(DateOfBirth);
}
public static int GetAge(DateTime dateOfBirth)
{
...
}
private async void LoadDataFromFirebase()
{
UserData = new ObservableCollection<FirebaseObject<LocalUser>>();
var firebase = new FirebaseClient(
"https://transcribed-c69c8-default-rtdb.europe-west1.firebasedatabase.app/");
var FireUser = await firebase
.Child("Users")
.OnceAsync<LocalUser>();
UserData.Clear();
foreach (var item in FireUser)
{
if (item.Object.Id == UserId)
{
UserData.Add(item);
}
}
if (UserData.Count > 0)
{
var user = UserData.First().Object;
DateOfBirth = user.DateOfBirth;
Email = user.Email;
}
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it. Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.