0

(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);
                }
            }
        }
    }
}

  • There is no `Excersises` property in class Workout, just an `excersises` field. Note that the correct spelling would be `Exercises`. So it should read `public List Exercises { get; } = new List();` – Clemens Mar 24 '22 at 12:16
  • ohh sorry i had changed it to excersises but also then its not doing something. How is t possible to come by the List exercise.Name in xml? – C.Bighouse Mar 24 '22 at 12:58
  • `ItemsSource="{Binding Exercises}"` on the inner ItemsControl should just work when Exercises is a property instead of a field. – Clemens Mar 24 '22 at 13:07

0 Answers0