none
Grid.Row Binding RRS feed

  • Question

  • I've been trying to set the Grid.Row property of a TextBlock (or any other element for that matter) through a binding expression and having no luck.  Would anyone have any advice on this, the situation can be simplified to something like this:

    Code Snippet

    public class DataItem
    {

    public string Summary { get; set; }
    public int Row { get; set; }
    public int Col { get; set; }
    }

    And the following mark up file.

    Code Snippet

    <ListBox ItemsSource="{StaticResource DataItems}">

    <ListBox.ItemsPanel>
    <ItemsPanelTemplate>
    <Grid>
    <!-- Row & Column definitions -->
    </Grid>
    </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemsTemplate>
    <TextBlock Text="{Binding Summary}" Grid.Row="{Binding Path=Row}" Grid.Column="{Binding Path=Col}" />
    </ListBox.ItemsTemplate>
    </ListBox>

    This does nothing to re-arrange the items onto the grid. Can anyone provide some insight?

    Thanks in advance.
    Saturday, April 12, 2008 8:03 PM

Answers

  • This works pretty well for me:

    Code Snippet

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:s="clr-namespace:System;assembly=mscorlib">
        <Page.Resources>
            <x:Array Type="{x:Type s:Int32}" x:Key="data">
                <s:Int32>0</s:Int32>
                <s:Int32>1</s:Int32>
                <s:Int32>2</s:Int32>
            </x:Array>
        </Page.Resources>
        <ListBox ItemsSource="{StaticResource data}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="23"/>
                            <RowDefinition Height="23"/>
                            <RowDefinition Height="23"/>
                        </Grid.RowDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Grid.Row" Value="{Binding}"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Page>



    You need to set the Grid.Row property on ListBoxItem instead.

    hope this helps
    Monday, April 14, 2008 9:32 AM

All replies

  • This works pretty well for me:

    Code Snippet

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:s="clr-namespace:System;assembly=mscorlib">
        <Page.Resources>
            <x:Array Type="{x:Type s:Int32}" x:Key="data">
                <s:Int32>0</s:Int32>
                <s:Int32>1</s:Int32>
                <s:Int32>2</s:Int32>
            </x:Array>
        </Page.Resources>
        <ListBox ItemsSource="{StaticResource data}">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="23"/>
                            <RowDefinition Height="23"/>
                            <RowDefinition Height="23"/>
                        </Grid.RowDefinitions>
                    </Grid>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Grid.Row" Value="{Binding}"/>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Page>



    You need to set the Grid.Row property on ListBoxItem instead.

    hope this helps
    Monday, April 14, 2008 9:32 AM
  • Thanks, works perfectly.

     

    Sorry for late reply, I'm so used to CodeProject's method of emailing when an answer is received that I only just thought to check back and see if it was answered.

    Thursday, April 17, 2008 3:06 PM