I want to display the data from my TelemetryDataPoint
inside the VM to the View, just for extra information, the TelemetryDataPoint
received the data from my Helper clas. I already tried with my code below, but somehow the data won't displayed to my View but if I debug TelemetryDataPoint
it has the value on it.
TelemetryDataPointVM.cs
public class TelemetryDataPointVM : INotifyPropertyChanged
{
private TelemetryDataPoint? telemetryDataPoint;
public TelemetryDataPoint? TelemetryDataPoint
{
get => telemetryDataPoint;
set
{
// when I checked the value below it has the value
telemetryDataPoint = value;
OnPropertyChanged(nameof(TelemetryDataPoint));
}
}
public TelemetryDataPointVM()
{
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged(string propertyName)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
HelperClass.cs
public class GetPortHelper
{
TelemetryDataPointVM TelemetryDataPointVM { get; set; }
public GetPortHelper()
{
TelemetryDataPointVM = new();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
if(sp.IsOpen)
{
string DataString = sp.ReadLine();
string[] arrayDataString = DataString.Split(",");
if(arrayDataString[3] == "C")
{
TelemetryDataPointVM.TelemetryDataPoint = ParseToTelemetryData(arrayDataString);
}
else if(arrayDataString[3] == "Y")
{
//ParseToTetheredData(arrayDataString);
}
}
}
}
Altitude.xaml
<UserControl x:Class="GUI_Cansat.View.Altitude"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GUI_Cansat.View"
xmlns:vm="clr-namespace:GUI_Cansat.ViewModel"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.DataContext>
<vm:TelemetryDataPointVM/>
</UserControl.DataContext>
<Grid>
<Label Content="{Binding TelemetryDataPoint.Altitude, Mode=TwoWay}"
ContentStringFormat="Altitude: {0} M"
Style="{StaticResource fontMain}"
VerticalAlignment="Center" FontSize="14"/>
</Grid>
Update 1:
I assembled my Altitude
in my MainWindows
like this:
<Border Style="{StaticResource borderMain}"
Grid.Row="8">
<view:Altitude x:Name="Altitude" />
</Border>
Should I put the DataContext
inside this <view:Altitude/>
? If I put the code like this and {Binding TelemetryDataPointVM}
, my VS told me "No Data context found for binding"