数据绑定后如何添加项目? [英] How do I add an item after databinding?

查看:20
本文介绍了数据绑定后如何添加项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些数据绑定到 gridview,然后在最后有一个按钮(类似于 Windows 8 上的 MSN Finance 应用程序).

I'm attempting to bind some data to a gridview, then have a button at the end (similar to the MSN Finance app on Windows 8).

我将如何实现这一目标?

How would I achieve this?

我当前的 xaml 代码:

My current xaml code:

            <DataTemplate>
                <GridView
                    x:Name="StopGridViewItem"
                    ItemsSource="{Binding}"
                    Margin="0,0,20,20"
                    AutomationProperties.AutomationId="ItemGridView"
                    AutomationProperties.Name="Items In Group"
                    ItemTemplate="{StaticResource Standard310x260ItemTemplate}"
                    SelectionMode="None"
                    IsSwipeEnabled="false"
                    IsItemClickEnabled="True" ItemClick="StopGridViewItem_ItemClick">
                </GridView>


            </DataTemplate>

提前致谢!

推荐答案

有很多很多方法可以处理这种情况.

There are many, many ways to handle this scenario.

如果您正在处理一个简单的列表,而不是我们在屏幕截图右侧看到的动态马赛克,您只需在列表后面放置一个按钮,样式使其看起来像以下项目之一你的清单.当您处理一个不会自动滚动并且是最语义化的解决方案时,这非常有效.

If you are dealing with a simple list and not a dynamic mosaic like the one we see on the right of your screenshot, you can simply place a button just after your list, styled so that it looks like one of the items of your list. This works really well when you are dealing with a list that does not scroll automatically and is the most semantic solution.

<DataTemplate>
    <StackPanel>
        <ItemsControl
            x:Name="StopGridViewItem"
            ItemsSource="{Binding}"
            ItemTemplate="{StaticResource Standard310x260ItemTemplate}">
        </ItemsControl>

        <Button ... />
    </StackPanel>
</DataTemplate>

如果您需要支持滚动,或者正在处理不能像马赛克一样简单地将按钮进一步向下推的控件,则需要在绑定到列表的集合中添加一个虚拟项目.如果列表虚拟化不是问题,您可以简单地将它附加到转换器内的集合末尾.如果您需要支持虚拟化,那么这不是一个解决方案.您将需要一个自定义控件来处理这种情况.

If you need to support scrolling, or are dealing with a control that cannot simply push your button further down like a mosaic, you need to add a dummy item inside the collection that you bind to your list. If list virtualization is not an issue, you could simply append it at the end of your collection inside a converter. If you need to support virtualization, then this is not a solution. You will need a custom control to handle this case.

最后,一旦将虚拟项目插入列表中,您可以在视图中生成项目时使用 DataTemplateSelector 在普通模板和虚拟模板之间切换.

Finally, once the dummy item is inserted in the list, you can use a DataTemplateSelector to switch between your normal template and your dummy template when generating the item in the view.

public static class EnumerableExtensions
{
    public static IEnumerable Append(this IEnumerable source, object o)
    {
        foreach (var x in source)
        {
            yield return x;
        }

        yield return o;
    }
}

public class DummyInserter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var source = value as IEnumerable;
        if (source == null)    throw new UnsupportedException("DummyInserter converter requires an IEnumerable source");

        return source.Append(new Dummy()).ToArray();
    }
}

对于 XAML:

<Resources>
    <DummyInserter x:key="AddButtonDummyInserter" />
    <DummyTemplateSelector x:key="MySelector"
                           DefaultTemplate="{StaticResource Standard310x260ItemTemplate}"
                           DummyTemplate="{StaticResource 310x260AddButtonTemplate}" />
</Resources>

<DataTemplate>
    <StackPanel>
        <ItemsControl
            x:Name="StopGridViewItem"
            ItemsSource="{Binding, Converter={StaticResource AddButtonDummyInserter}}"
            ItemTemplateSelector="{StaticResource MySelector}">
        </ItemsControl>

        <Button ... />
    </StackPanel>
</DataTemplate>

这篇关于数据绑定后如何添加项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆