WPF GridView控件具有动态清晰度 [英] WPF GridView with a dynamic definition

查看:151
本文介绍了WPF GridView控件具有动态清晰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个ListView的GridView的模式显示了一组数据,我的程序将被从外部源接收。该数据将包括两个阵列,列名之一和字符串值中的一个的填充控制

I want to use the GridView mode of a ListView to display a set of data that my program will be receiving from an external source. The data will consist of two arrays, one of column names and one of strings values to populate the control.

我不知道如何创建,我可以作为一个ListView项目使用合适的类。我知道填充项的唯一方法是将其设置为与重新present列属性的类,但我有运行时前不列的知识。

I don't see how to create a suitable class that I can use as the Item in a ListView. The only way I know to populate the Items is to set it to a class with properties that represent the columns, but I have no knowledge of the columns before run-time.

我可以在描述动态创建一个ItemTemplate:<一href=\"http://stackoverflow.com/questions/125638/create-wpf-itemtemplate-dynamically-at-runtime\">http://stackoverflow.com/questions/125638/create-wpf-itemtemplate-dynamically-at-runtime但它仍然使我在不知如何描述实际的数据。

I could create an ItemTemplate dynamically as described in: http://stackoverflow.com/questions/125638/create-wpf-itemtemplate-dynamically-at-runtime but it still leaves me at a loss as to how to describe the actual data.

任何帮助感激地接受。

推荐答案

您可以添加GridViewColumns到GridView动态给出了使用方法是这样的第一个数组:

You can add GridViewColumns to the GridView dynamically given the first array using a method like this:

private void AddColumns(GridView gv, string[] columnNames)
{
    for (int i = 0; i < columnNames.Length; i++)
    {
        gv.Columns.Add(new GridViewColumn
        {
            Header = columnNames[i],
            DisplayMemberBinding = new Binding(String.Format("[{0}]", i))
        });
    }
}

我假设包含值的第二个数组将ROWS *柱长度。在这种情况下,你的项目可以是长度列的字符串数组。您可以使用Array.Copy或LINQ分裂数组。其原理是在这里证明了:

I assume the second array containing the values will be of ROWS * COLUMNS length. In that case, your items can be string arrays of length COLUMNS. You can use Array.Copy or LINQ to split up the array. The principle is demonstrated here:

<Grid>
    <Grid.Resources>
        <x:Array x:Key="data" Type="{x:Type sys:String[]}">
            <x:Array Type="{x:Type sys:String}">
                <sys:String>a</sys:String>
                <sys:String>b</sys:String>
                <sys:String>c</sys:String>
            </x:Array>
            <x:Array Type="{x:Type sys:String}">
                <sys:String>do</sys:String>
                <sys:String>re</sys:String>
                <sys:String>mi</sys:String>
            </x:Array>
        </x:Array>
    </Grid.Resources>
    <ListView ItemsSource="{StaticResource data}">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path=[0]}" Header="column1"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=[1]}" Header="column2"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=[2]}" Header="column3"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

这篇关于WPF GridView控件具有动态清晰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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