(i'm new to WPF.)
I'm trying to make a WorkoutPlanner application in WPF with a EF core API. Now i have a problem. I have a Workout and excersise model like:
public class Workout
{
public string Name { get; set; }
public DateTime Created { get; set; }
public List<Excersise> excersises = new List<Excersise>();
}
public class Excersise
{
public string Name { get; set; }
}
Now i want to Bind the excersise Name in a itemsControl in de XML. That is now looking so:
<ItemsControl ItemsSource="{Binding Workouts}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock
Text="{Binding Name}"
HorizontalAlignment="Center"
VerticalAlignment="Top"
FontSize="16"
TextWrapping="Wrap"
TextAlignment="Center"
FontWeight="DemiBold"
Width="150"
Height="150" Foreground="White" />
<ItemsControl ItemsSource="{Binding Excersises}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
The data of the workout is showing on the screen but the excersises is not. Does someone know how to fix this in my ViewModel???
ViewModel
public class HomeViewModel : BindableBase
{
ObservableCollection<Workout> workouts;
public ObservableCollection<Workout> Workouts
{
get { return workouts; }
set
{
SetProperty(ref workouts, value);
OnPropertyChanged(nameof(Workouts));
}
}
public DelegateCommand GetWorkoutsClicked { protected set; get; }
public HomeViewModel()
{
Workouts = new ObservableCollection<Workout>();
GetWorkoutsClicked = new DelegateCommand(ExecuteGetWorkouts);
}
private async void ExecuteGetWorkouts(object parameter)
{
var getAllWorkouts = await WebAPI.GetCall("workouts");
if (getAllWorkouts.StatusCode == System.Net.HttpStatusCode.OK)
{
var allWorkouts = getAllWorkouts.Content.ReadAsAsync<IEnumerable<Workout>>().Result;
foreach (Workout workout in allWorkouts)
{
Workouts.Add(workout);
}
}
}
}
}