使用Gridview进行数据绑定

时间:2012-04-13 17:02:05

标签: .net xaml data-binding gridview windows-runtime

我正在使用Gridview显示我从WCF服务获得的一些事件项。 我使用的代码

代码隐藏:

List<GroupInfoList<object>> groups = new List<GroupInfoList<object>>();
ServiceReference1.Service1Client c = new ServiceReference1.Service1Client();
List<Tag> tags=(await c.GetUserTagsAsync(u.id)).ToList();
var q = from t in tags
                orderby ((Tag)t ).tag1
                group t by ((Tag)t).tag1 into g
                select new { GroupName = g.Key, Items = g };
        foreach (var g in q)
        {
            GroupInfoList<object> info = new GroupInfoList<object>();
            info.Key = g.GroupName;
            foreach (var item in g.Items)
            {
                info.Add(item);
            }
            groups.Add(info);
        }
        cvs1.Source = groups;

public class GroupInfoList<T> : List<object>
{

    public object Key { get; set; }


    public new IEnumerator<object> GetEnumerator()
    {
        return (System.Collections.Generic.IEnumerator<object>)base.GetEnumerator();
    }
}

WCF签名:

List<Event> GetEventsByTag(string tag);   
List<Tag> GetUserTags(Guid uid); 

XAML

<GridView x:Name="ItemsByCategory"  VerticalAlignment="Bottom" 
                                  ItemsSource="{Binding Source={StaticResource cvs1}}"
                                  BorderBrush="Gray" BorderThickness="1">

        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>

        <GridView.ItemTemplate>
            <DataTemplate>
                <Grid HorizontalAlignment="Left" Background="White">
                    <StackPanel Orientation="Horizontal" Margin="10,10,0,0">                            
                        <Image Source="{Binding Image}" Height="60" Width="60" VerticalAlignment="Center" Margin="0,0,10,0"/>
                        <StackPanel Margin="0,0,0,0" Orientation="Vertical">
                            <TextBlock TextWrapping="Wrap" Style="{StaticResource ItemTitleStyle}"  Width="200" VerticalAlignment="Center" Text="{Binding XXXXXXXX}" 
                                       HorizontalAlignment="Left" FontFamily="Segoe UI" />
                        </StackPanel>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>

        <GridView.GroupStyle>
            <GroupStyle>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <Grid Background="White" Margin="0">
                            <TextBlock Text='{Binding Key}' Foreground="Gray" FontSize="25" Margin="5" />
                        </Grid>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>

                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
            </GroupStyle>
        </GridView.GroupStyle>
    </GridView>

问题是我不知道将什么放入TextBlock中的数据绑定(标有XXX)所以我可以将它绑定到我的源中每个事件的title属性?

1 个答案:

答案 0 :(得分:1)

最后修好了

在C#中我将代码修改为像这样

foreach (var g in q)
        {
            GroupInfoList<object> info = new GroupInfoList<object>();
            info.Key = g.GroupName;
            foreach (var item in g.Items)
            {
                foreach (var ev in item.Events)
                {
                    info.Add(ev);
                }
            }
            groups.Add(info);

和XAML

<TextBlock TextWrapping="Wrap" Style="{StaticResource ItemTitleStyle}"  Width="200" VerticalAlignment="Center" Text="{Binding title}" 
                                       HorizontalAlignment="Left" FontFamily="Segoe UI" />