views:

23

answers:

1

I'm working on an app that has rows in the following format. I've numbered the rows for clarity. The rows alternate background, so even rows are all one color and odd rows are another. I've it working using a grid containing x number of rows, and within each row i create another grid with two rows and two columns to organize the data. I'm new to WP7 dev and have a little experience with Silverlight. It seems like there should be an easier way of doing this, but I haven't found a suitable user control. It feels like I'm doing this the hard way, but I haven't found a user control designed for this layout.

--------------------------------
1       label: data
               more data
--------------------------------
2       label: data
               more data
--------------------------------
3       label: data
               more data
--------------------------------
A: 

You could do this with a ListBox rather than nested grids.
For example, create a new list project and do the following:

In mainpage.xaml, change line 35 to be the following:

<StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal" Background="{Binding Converter={StaticResource myconverter}}">

Add the following to app.xaml:

<Application.Resources>
    <alternateRows:MyConverter x:Key="myconverter" />
</Application.Resources>

add a new class called MyConverter with the following

public class MyConverter: IValueConverter
{
    bool flag = false;

    SolidColorBrush brush1 = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
    SolidColorBrush brush2 = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        flag = !flag;
        return flag ? brush1 : brush2;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

This should server as a working example that you can adjust as you need.

Matt Lacey
Thanks, as for the lining up the label with the two rows of data, should I keep using an embedded grid or is there another control that allows you to do that with less work?
CACuzcatlan
A grid seems perfectly acceptable. It's grids in grids which ring alarm bells for me. There are also better alternatives to grids with only a single row or column. Such as a StackPanel or ListBox (depending on your needs). In your case you could consider a horizontally aligned stackpanel. Also, please vote up or accept the answer if it helped.
Matt Lacey