I have a hash table and I want to bind it to a ListView in wpf in the code or code behind.
My ListView is
<ListView Canvas.Left="1045" Canvas.Top="634" Height="244" Name="lvContact" Width="536" >
<ListView.View>
<GridView x:Name="gvContacts">
<GridView.Columns>
<GridViewColumn Width="200" x:Name="ContactName" DisplayMemberBinding="{Binding Path=Username}"></GridViewColumn>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
On the code beind I have no idea how to bind it, but before I was using this in a Windows Forms app
//foreach (DictionaryEntry de in contactList)
//{
// ListViewItem contactItem = new ListViewItem(de.Key.ToString());
// if (de.Value.ToString() == "NLN")
// {
// contactItem.ForeColor = System.Drawing.Color.Green;
// }
// else
// {
// contactItem.ForeColor = System.Drawing.Color.Gray;
// }
// lvContact.Items.Add(contactItem);
//}
But now it doesnt work properly please help
You’re just missing the standard binding. Here’s the XAML for a ListBox:
And here’s the XAML for a DataGrid:
In your view model (or code-behind), you need to have your source data:
And:
Just google or read about binding and you’ll be all set.