Reputation: 3238
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
Upvotes: 0
Views: 1902
Reputation: 34297
You're just missing the standard binding. Here's the XAML for a ListBox:
<ListBox DockPanel.Dock="Bottom" ItemsSource="{Binding Applications}" DisplayMemberPath="Name"
SelectedItem="{Binding SelectedApplication}" Height="auto"/>
And here's the XAML for a DataGrid:
<DataGrid Height="280" AutoGenerateColumns="False" IsReadOnly="True" HeadersVisibility="Column"
ItemsSource="{Binding SelectedApplication.Tasks}"
SelectedItem="{Binding SelectedTask}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Sequence}" Header="Order" Width="50" />
<DataGridTextColumn Binding="{Binding Path=Description}" Header="Description" Width="*"/>
<DataGridTextColumn Binding="{Binding Path=TaskType}" Header="Type" Width="120"/>
<DataGridTextColumn Binding="{Binding Path=FailureCausesAllStop}" Header="Stop" Width="50"/>
</DataGrid.Columns>
</DataGrid>
In your view model (or code-behind), you need to have your source data:
public Collection<Application> Applications
{
get { return this._applications; }
private set
{
this._applications = value;
this.NotifyPropertyChanged(() => this.Applications);
}
}
And:
public Application SelectedApplication
{
get { return this._selectedApplication; }
set
{
this._selectedApplication = value;
this.NotifyPropertyChanged(() => this.SelectedApplication);
}
}
Just google or read about binding and you'll be all set.
Upvotes: 1