code for 'Data'
public ObservableCollection<Graph> Data { get; set; }
Graph object:
{public class Graph : INotifyPropertyChanged
{
private TimeSpan time;
private double units;
public event PropertyChangedEventHandler PropertyChanged;
public TimeSpan Time
{
get { return time; }
set
{
time = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Time"));
}
}
public double Units
{
get { return units; }
set
{
units = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Time"));
}
}
}
'Data' is a list of 'Graph' objects and I need to display the value of the 'Unit' in the last Graph object of the list, 'Data'. How can I do it in xaml with data binding? This is what I tried and it doesn't work:
<Label Grid.Row="0" Grid.Column="2" Text="{Binding Data[Data.Count-1].Units}"/>
Really appreciate the help! Thanks a lot!
CodePudding user response:
you can use a converter for this.
CodePudding user response:
You could try the code below.
ViewModel:
public class GraphViewModel
{
public ObservableCollection<Graph> Data { get; set; }
public GraphViewModel()
{
Data = new ObservableCollection<Graph>()
{
new Graph(){ Time = new TimeSpan(0,0,0), Units=1 },
new Graph(){ Time = new TimeSpan(0,0,0), Units=2 },
new Graph(){ Time = new TimeSpan(0,0,0), Units=3 },
new Graph(){ Time = new TimeSpan(0,0,0), Units=4 },
new Graph(){ Time = new TimeSpan(0,0,0), Units=5 },
};
}
}
Xaml:
<ContentPage.Content>
<Label Text="{Binding .}"/>
</ContentPage.Content>
Code behind:
public partial class Page4 : ContentPage
{
GraphViewModel viewModel;
public Page4()
{
InitializeComponent();
viewModel = new GraphViewModel();
this.BindingContext = viewModel.Data[viewModel.Data.Count - 1].Units;
}
}
