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 9225365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:33:30+00:00 2026-06-18T04:33:30+00:00

Here’s some context from a related post of mine: How to databind using to

  • 0

Here’s some context from a related post of mine:
How to databind using to a 2d array of objects of different types

So I have a:

List<List<FarmyardSpace>>

I want this to be represented as buttons in cells of a Grid. I can’t use UniformGrid as in the related post because I need to define the sizes of the cells in the Grid. Ideally I don’t want to have to define Row and Column fields in my data (although perhaps this should be handled by the ViewModel somehow? Just started learning this stuff the other day and I’m still wrapping my head around WPF and MVVM).

Here’s my latest attempt which does not work (and in fact throws an exception), and in this particular example I’m relying on Column and Row properties existing on the FarmyardSpaces:

<ItemsControl ItemsSource="{Binding FarmyardGrid}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="15"/>
                            <RowDefinition />
                            <RowDefinition Height="15"/>
                            <RowDefinition />
                            <RowDefinition Height="15"/>
                            <RowDefinition />
                            <RowDefinition Height="15"/>
                        </Grid.RowDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ContentPresenter">
                        <Setter Property="Grid.Row" Value="{Binding Row}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="15"/>
                    <ColumnDefinition />
                    <ColumnDefinition Width="15"/>
                    <ColumnDefinition />
                    <ColumnDefinition Width="15"/>
                    <ColumnDefinition />
                    <ColumnDefinition Width="15"/>
                    <ColumnDefinition />
                    <ColumnDefinition Width="15"/>
                    <ColumnDefinition />
                    <ColumnDefinition Width="15"/>
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Grid.Column" Value="{Binding Column}"/>
        </Style>
    </ItemsControl.ItemContainerStyle>
</ItemsControl>

I think part of the reason this doesn’t work is that the Items contained by the outer ItemsControl are not FarmyardSpaces but List, so they don’t have a Column property to bind to. I’ve also tried it with both the RowDefinitions and ColumnDefinitions in the inner ItemsControl. This gets rid of the exception but also doesn’t work and seems wrong to me, especially given the solution that works when using UniformGrid in the related post where the Columns are declared in the outer ItemsControl.

Anyways I’d appreciate any help figuring this out, thanks in advance!

  • 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-06-18T04:33:32+00:00Added an answer on June 18, 2026 at 4:33 am

    If you have properties on your collection items that specify where in the grid they go that’s obviously easiest, and that’s what you are set up for now. If not, you can use a converter to figure out each item’s index for you and then assign that as the Row/Column value – and it can work the same in both dimensions. Here’s the basic converter:

    public class ItemToIndexConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            object item = values.FirstOrDefault();
            IList collection = values.OfType<IList>().LastOrDefault();
    
            if (collection == null || item == null)
                return 0;
    
            return collection.IndexOf(item);
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    and modified version of your XAML (cut down to 3×3) getting the current item and the full collection from the parent ItemsControl to pass into the converter:

    <ItemsControl ItemsSource="{Binding FarmyardGrid}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="15"/>
                                    <RowDefinition />
                                    <RowDefinition Height="15"/>
                                </Grid.RowDefinitions>
                            </Grid>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="ContentPresenter">
                            <Setter Property="Grid.Row">
                                <Setter.Value>
                                    <MultiBinding>
                                        <MultiBinding.Converter>
                                            <local:ItemToIndexConverter/>
                                        </MultiBinding.Converter>
                                        <Binding/>
                                        <Binding Path="ItemsSource"
                                                 RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}"/>
                                    </MultiBinding>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="15"/>
                        <ColumnDefinition />
                        <ColumnDefinition Width="15"/>
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Grid.Column">
                    <Setter.Value>
                        <MultiBinding>
                            <MultiBinding.Converter>
                                <local:ItemToIndexConverter/>
                            </MultiBinding.Converter>
                            <Binding/>
                            <Binding Path="ItemsSource"
                                     RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}"/>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the script I'm using, copied directly from Google: <script type=text/javascript> var _gaq
Here is a scenario: User installs .NET application that you have made. After some
Here is the code from my project where I am using a datepicker. The
Here is what I am currently doing. PHP echo's out the recent post in
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
I have just tried to save a simple *.rtf file with some websites and
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
Here are two ways of setting context: $.proxy(function() { this.doStuff(); }, this); ... var
Here is what I am getting when I use single line comment (using --
Here's my jQuery script: $.ajax({ type: 'POST', url: 'zipCodes.php', data: searchZipCode, cache: false, success:

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.