Yanshof
Yanshof

Reputation: 9926

How to bind between listView with two column and database items?

I define some listview that each row contain 2 button. Each button contain text that exist in the sql server database.

I want to bind the button text to the database - but i don't know how to do it because of the special listview structure.

Upvotes: 0

Views: 391

Answers (1)

devdigital
devdigital

Reputation: 34349

You should set the ItemsSource property of the ListView (any particular reason you're not using a ListBox?) to a collection containing domain models which have been populated by the database. If you are using codebehind, then you can use listViewName.ItemsSource = myListOfDomainObjects. If you are using MVVM, then expose a property on your viewmodel, and use a binding expression in the XAML.

<ListView x:Name="MyList" ItemsSource="{Binding MyViewModelCollectionProperty}"> 
  ...
</ListView>

If your domain model has an e.g. 'Name' string property, then you can use this as the button text in your ItemTemplate by using:

<Button Content="{Binding Name}" />

Upvotes: 1

Related Questions