0
votes

I want to add panels dynamically into grid based on some count in wpf. But not in code behind or programatically. Using xaml only i need to create panels into grid dynamically. Is there any framework to do this?. Please help me.

1

1 Answers

1
votes

Using a ViewModel from the MVVM pattern, create an ObservableCollection and bind the ItemsSource for an ItemsControl to the ObservableCollection.

public class ViewModel : INotifyPropertyChanged
{ 
  public ViewModel() { Objects = new ObservableCollection<object>(); }

  public ObservableCollection<object> Objects { get;set; }
}

Then in the View:

<Grid>
  <ItemsControl ItemsSource="{Binding Path=Objects}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <Panel />
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  <ItemsControl>
</Grid>

Then in the codebehind on the view:

DataContext = new ViewModel();

This will create one panel per item in your ObservableCollection.