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.
If you are setting DataContext, you have to set the ItemsSource property to bind data.
Otherwise, you can bind the ItemsSource directly as follows: