动态构建列表视图 [英] Build listview dynamically

查看:87
本文介绍了动态构建列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个listview,我想在运行时动态构建它,因为我想使用同一个窗口显示包含不同字段的两个不同列表。



我该怎么做呢?



I have this listview and I would like to build it dynamically at runtime as I would like to use the same window to display two different lists containing different fields.

How do I go about doing this?

<ListView.View>
    <GridView>
        <GridViewColumn Width="140"  DisplayMemberBinding="{Binding GameName}">
            <GridViewColumnHeader Tag="GameName" Click="lvUsersColumnHeader_Click">GameName</GridViewColumnHeader>
        </GridViewColumn>
        <GridViewColumn Width="140"  DisplayMemberBinding="{Binding Creator}">
            <GridViewColumnHeader Tag="Creator" Click="lvUsersColumnHeader_Click">Creator</GridViewColumnHeader>
        </GridViewColumn>
        <GridViewColumn Width="140"  DisplayMemberBinding="{Binding Publisher}">
            <GridViewColumnHeader Tag="Publisher" Click="lvUsersColumnHeader_Click">Publisher</GridViewColumnHeader>
        </GridViewColumn>
    </GridView>
</ListView.View>

推荐答案

您可以通过XAML执行的所有操作都可以通过代码执行(反之亦然)。 RyanDev已经在他的评论中为您提供了解决方案。



我可以根据上面的注释向您提供另一个非常普遍的建议,并了解XAML的作用。使用XAML开发UI时,项目的所有XAML代码仅用于生成普通C#代码(在您的情况下为C#)。当您只构建项目而不启动应用程序时,所有自动生成的代码都已创建和编译。只需查看此代码即可了解其功能。您可以在项目的obj子目录下以常规* .cs文件的形式找到它。



每次都可以使用这种方法您知道如何在XAML中执行某些操作但不知道如何在代码中执行此操作。在XAML示例中创建所需的效果,然后查看已生成的代码。



-SA
Everything you can do by XAML you can do by code (and not visa versa). RyanDev already provided you the solution in his comment.

I can give you another, the very universal advice based on my note above and understanding what XAML does. When you develop UI with XAML, all XAML code of your project is just used to generate "normal" C# code (C#, in your case). When you just build the project and not even start the application, all that auto-generated code is already created and compiled. Just look at this code to see what it does. You will find it in the form of "regular" *.cs file under the "obj" sub-directory of your project.

You can use this approach every time you know how to do something in XAML but don't know how to do it in code. Create the desired effect in XAML sample and then see what code has been generated.

—SA


这篇关于动态构建列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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