在不使用数据绑定的情况下将行添加到Gridview

时间:2009-10-06 19:30:25

标签: wpf arrays gridview listview rows

我在xaml预定义的listview中有一个gridview:

...

<ListView x:Name="listPriority" IsSynchronizedWithCurrentItem="True" Margin="0,30,0,4" BorderThickness="0,0,0,0">
<ListView.View>
    <GridView>
        <GridViewColumn x:Name="grvPriorityColumn" Width="140" Header="Priority" />
        <GridViewColumn x:Name="grvMessage" Width="250" Header="Message" />
    </GridView>
</ListView.View>

...

我有一个数组,其中包含需要进入“grvPriorityColumn”和“grvMessage”的数据。如何在不使用数据绑定的情况下将此数据添加到gridview?

1 个答案:

答案 0 :(得分:1)

我认为没有办法直接将值分配给GridView列,但您可以使用匿名对象绑定到这些列:

var priorities = new int [] { 3, 4, 5 };
var messages = new string [] { "first", "second", "third" };
var items = new List<object>();

for (int i = 0; i < priorities.Length; i++) {
  items.Add(new { Priority = priorities[i], Message = messages[i] });
}

listPriority.ItemsSource = items;
在XAML中

<ListView x:Name="listPriority">
<ListView.View>
        <GridView>
                <GridViewColumn DisplayMemberBinding="Priority" Header="Priority" />
                <GridViewColumn DisplayMemberBinding="Message" Header="Message" />
        </GridView>
</ListView.View>
</ListView>