views:

305

answers:

1

Hello,

I have a gridview inside of a listview predefined in the xaml:

....

<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>

....

I have an array containing data that needs to go into the "grvPriorityColumn" and "grvMessage". How do add this data to the gridview without using databinding?

A: 

I don't think there's a way to directly assign values into GridView's columns, but you can use anonymous objects to be bound to those columns:

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;

in XAML:

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