James Jeffery
James Jeffery

Reputation: 557

WPF8/C# - Binding Data To Grid

I understand I posted this question, but having accepted the answer on my last question and following through the article I realized it wasn't the answer I was looking for. I've posted again with some sample code.

I want to fill a Grid (not a DataGrid) with Data from a collection. Here is what I have but it does not work. If I remove the collection and set the DataContext to a single object it works, but not as a collection.

XAML

Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <StackPanel>
            <TextBlock Text="{Binding Path=StudentName}" />
        </StackPanel>
</Grid>

MainPage.xaml.cs

public MainPage()
    {
        InitializeComponent();

        ObservableCollection<Student> ob = new ObservableCollection<Student>();

        ob.Add(new Student()
        {
            StudentName = "James Jeffery"
        });

        ob.Add(new Student()
        {
            StudentName = "Sian Ellis"
        });



        this.DataContext = ob;

        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }

This has been bugging me for hours. I just can't seem to fill a grid with a collection. Every example on Google shows ListViews etc. I want to fill a Grid, and only a Grid.

Any advice on how to achieve this?

Upvotes: 2

Views: 2290

Answers (2)

Fede
Fede

Reputation: 44068

As mentioned in another answer, you need an ItemsControl:

<Window x:Class="MiscSamples.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <ItemsControl ItemsSource="{Binding}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid IsItemsHost="True" Rows="3" Columns="3"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBox Text="{Binding Name}" Margin="2"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Window>

Code Behind:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new List<Student>
                {
                    new Student() {Name = "James Jeffery"},
                    new Student() {Name = "Sian Ellis"},
                    new Student() {Name = "James Jeffery 2"},
                    new Student() {Name = "Sian Ellis 2"},
                    new Student() {Name = "James Jeffery 3"},
                    new Student() {Name = "Sian Ellis 3"},
                };
        }
    }

Output:

enter image description here

Upvotes: 1

weston
weston

Reputation: 54811

You cant. Grid is not capable of this. You need to use ItemsControl or a desendant of ItemsControl.

Try this tutorial: http://www.galasoft.ch/mydotnet/articles/article-2007041201.aspx

Upvotes: 0

Related Questions