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

  • SEARCH
  • Home
  • 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 6215293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:01:50+00:00 2026-05-24T07:01:50+00:00

I want to bind a collection of objects to a DataGrid in Silverlight. The

  • 0

I want to bind a collection of objects to a DataGrid in Silverlight. The objects belong to the following type:

public class Seats
{
    Dictionary<Group, long> dctValues = new Dictionary<Group, long>();

    public int Id { get; set; }

    public Dictionary<Group, long> Values
    {
        get { return dctValues; }
    }
}

Whereas, Group is represented by:

public class Group
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I want to be able to generate columns based on the dictionary of groups, where each column would have the header set to Group.Name and the cell value for each item equal to the long value in the dictionary.

  • 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. Editorial Team
    Editorial Team
    2026-05-24T07:01:50+00:00Added an answer on May 24, 2026 at 7:01 am

    I’m going to assume that we can’t guarantee that there is only a single instance of a Group class for each group name. (Else you would be generating columns based on a list of known groups no?)

    Here is a class derived from DataGrid:

    public class SeatsGrid : DataGrid, IValueConverter
    {
        public SeatsGrid()
        {
            AutoGenerateColumns = false;
        }
        #region public List<Seats> SeatsList
    
        public List<Seats> SeatsList
        {
            get { return GetValue(SeatsListProperty) as List<Seats>; }
            set { SetValue(SeatsListProperty, value); }
        }
    
        public static readonly DependencyProperty SeatsListProperty =
            DependencyProperty.Register(
                "SeatsList",
                typeof(List<Seats>),
                typeof(SeatsGrid),
                new PropertyMetadata(null, OnSeatsListPropertyChanged));
    
        private static void OnSeatsListPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SeatsGrid source = d as SeatsGrid;
            List<Seats> value = e.NewValue as List<Seats>;
            source.OnSeatsListPropertyChanged(value);
        }
    
        private void OnSeatsListPropertyChanged(List<Seats> value)
        {
            ItemsSource = null;
            Columns.Clear();
    
            var groups = value
                .SelectMany(seats => seats.Values.Keys)
                .Select(g => g.Name)
                .Distinct();
    
            foreach (var group in groups)
            {
                DataGridTextColumn col = new DataGridTextColumn();
                col.Binding = new Binding()
                {
                    Converter = this,
                    ConverterParameter = group
                };
                col.Header = group;
                Columns.Add(col);
            }
    
            ItemsSource = value;
        }
        #endregion public List<Seats> SeatsList
    
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Seats seats = (Seats)value;
            string group = (string)parameter;
    
            Group actualGroup = seats.Values.Keys.FirstOrDefault(g => g.Name == group);
            if (actualGroup != null)
            {
                return seats.Values[actualGroup].ToString();
            }
            else
            {
                return null;
            }
        }
    
        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Place an instance of this class in Xaml and assign a List<Seats> to its SeatsList property and it generates the columns and renders the rows.

    How does it work?

    The magic starts in the OnSeatsListPropertyChanged method. It first gets a list of distinct Group names. It generates a new Text column for each group name setting the header naturally to the group name.

    The weird stuff appears when setting the binding for the column. The binding is given a converter which for convience I decided to implement on the SeatsGrid class as well. The converter parameter is the group name. Since no path is specified the whole Seats object will be passed to the converter when binding actually occurs.

    Now looking at the IValueConverter.Convert method. It finds an instance (is any) of Group in the seats that has the same name as the converter parameter. If found uses that Group as the key to lookup a value to return.

    If the Groups were known to be unique per name then the code can be simplified but the principle is the same.

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

Sidebar

Related Questions

I have the following class that I want to bind it's dictionary's value to
I want to take a collection of objects and bind it to a StackPanel
I've got a collection of ViewModels and want to bind a ListBox to them.
I want to bind to a value in a dictionary property of an object.
I want to bind my ListView control to a generic list of objects? I
I want to bind an event to a certain class and ID for when
I'm binding a GridView to a collection of objects that look like this: public
I have a collection of objects I bind to a Listview like this: if
I have a collection of objects that I wish to bind to a ListView
I have a List<Collection<string>> object with 10,000 objects in it and I want to

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.