Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 81339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:26:40+00:00 2026-05-10T21:26:40+00:00

I have a 2-dimensional array of objects and I basically want to databind each

  • 0

I have a 2-dimensional array of objects and I basically want to databind each one to a cell in a WPF grid. Currently I have this working but I am doing most of it procedurally. I create the correct number of row and column definitions, then I loop through the cells and create the controls and set up the correct bindings for each one.

At a minimum I would like to be able to use a template to specify the controls and bindings in xaml. Ideally I would like to get rid of the procedural code and just do it all with databinding, but I’m not sure that’s possible.

Here is the code I am currently using:

public void BindGrid() {     m_Grid.Children.Clear();     m_Grid.ColumnDefinitions.Clear();     m_Grid.RowDefinitions.Clear();      for (int x = 0; x < MefGrid.Width; x++)     {         m_Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star), });     }      for (int y = 0; y < MefGrid.Height; y++)     {         m_Grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star), });     }      for (int x = 0; x < MefGrid.Width; x++)     {         for (int y = 0; y < MefGrid.Height; y++)         {             Cell cell = (Cell)MefGrid[x, y];                                  SolidColorBrush brush = new SolidColorBrush();              var binding = new Binding('On');             binding.Converter = new BoolColorConverter();             binding.Mode = BindingMode.OneWay;              BindingOperations.SetBinding(brush, SolidColorBrush.ColorProperty, binding);              var rect = new Rectangle();             rect.DataContext = cell;             rect.Fill = brush;             rect.SetValue(Grid.RowProperty, y);             rect.SetValue(Grid.ColumnProperty, x);             m_Grid.Children.Add(rect);         }     }  } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T21:26:41+00:00Added an answer on May 10, 2026 at 9:26 pm

    The purpose of the Grid is not for real databinding, it is just a panel. I am listing down the easiest way to accomplish the visualization of a two dimensional list

    <Window.Resources>     <DataTemplate x:Key='DataTemplate_Level2'>             <Button Content='{Binding}' Height='40' Width='50' Margin='4,4,4,4'/>     </DataTemplate>      <DataTemplate x:Key='DataTemplate_Level1'>         <ItemsControl ItemsSource='{Binding}' ItemTemplate='{DynamicResource DataTemplate_Level2}'>             <ItemsControl.ItemsPanel>                 <ItemsPanelTemplate>                     <StackPanel Orientation='Horizontal'/>                 </ItemsPanelTemplate>             </ItemsControl.ItemsPanel>         </ItemsControl>     </DataTemplate>  </Window.Resources> <Grid>     <ItemsControl x:Name='lst' ItemTemplate='{DynamicResource DataTemplate_Level1}'/> </Grid> 

    And in the code behind set the ItemsSource of lst with a TwoDimentional data structure.

      public Window1()     {         List<List<int>> lsts = new List<List<int>>();          for (int i = 0; i < 5; i++)         {             lsts.Add(new List<int>());              for (int j = 0; j < 5; j++)             {                 lsts[i].Add(i * 10 + j);             }         }          InitializeComponent();          lst.ItemsSource = lsts;     } 

    This gives you the following screen as output. You can edit the DataTemplate_Level2 to add more specific data of your object.

    alt text

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I tend towards first approach for all the usual reasons… May 11, 2026 at 10:56 am
  • added an answer You dont need 3.5 for the history control, check ScottGu's… May 11, 2026 at 10:56 am
  • added an answer You can start here: Red book You create tags for… May 11, 2026 at 10:56 am

Related Questions

I have a 2-dimensional array of objects and I basically want to databind each
I have a 2 dimensional array, like so: char[,] str = new char[2,50]; Now,
Hi I have a JSON object that is a 2-dimentional array and I need
I have a 2.4 MB XML file, an export from Microsoft Project (hey I'm
I have a class that processes a 2 xml files and produces a text
I have a .NET 2.0 windows forms app, which makes heavy use of the
I have a simple 2-column layout with a footer that clears both the right
I have a .NET 2.0 application that has recently had contributions that are Service
I have a .NET 2.0 Windows Forms application. Where is the best place the
I have a silverlight 2 beta 2 application that accesses a WCF web service.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.