Reputation: 525
I'm new to this and Google isn't helping me this time. I was able to follow some examples and populate a DataGrid and modify a database, but ListView is giving me a problem. Here is my class:
public class GlobalDataviews : INotifyPropertyChanged
{
...
//Billable data table
//Populated elsewhere with "SELECT ID, Value FROM BillableTable;"
private DataTable mBillable;
public DataView Billable()
{
return mBillable.DefaultView;
}
}
Here is my XAML snippets:
<Window.Resources>
<ObjectDataProvider x:Key="GlobalDataviews" ObjectType="{x:Type local:GlobalDataviews}" />
<ObjectDataProvider x:Key="BillableData" ObjectInstance="{StaticResource GlobalDataviews}" MethodName="Billable" />
</Window.Resources>
And now for my ListView:
<ListBox Name="listBox1" DataContext="{StaticResource BillableData}" SelectedValuePath="ID" DisplayMemberPath="Value"/>
I'm probably missing something very simple. What is the correct method? I would also like to bind the selected value (no multiselect) to another property from my code. Can anyone help? Not sure why I'm getting so confused.
Upvotes: 1
Views: 2708
Reputation: 4303
If you are setting DataContext, you have to set the ItemsSource property to bind data.
<ListBox x:Name="listBox1" DataContext="{StaticResource BusinessData}" ItemsSource="{Binding}" SelectedValuePath="ID" DisplayMemberPath="Value" />
Otherwise, you can bind the ItemsSource directly as follows:
<ListBox x:Name="listBox1" ItemsSource="{Binding Source={StaticResource BusinessData}}" SelectedValuePath="ID" DisplayMemberPath="Value" />
Upvotes: 2
Reputation: 45155
You are missing the binding for ItemsSource.
Something like this:
<ListBox Name="listBox1" DataContext="{StaticResource BusinessData}" ItemsSource="{Binding myCollection}" SelectedValuePath="ID" DisplayMemberPath="Value"/>
Where myCollection
is a property that exposes your list.
Upvotes: 1