Reputation: 310
I am trying to create a two column ListView with a data binding. The XAML looks like this:
<ListView x:Name="BookingVariantsListView" ItemsSource="{Binding BookingVariants}" SelectionChanged="BookingVariantsListView_SelectionChanged" DockPanel.Dock="Top" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Top">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridView.Columns>
<GridViewColumn Header="{x:Static loc:Settings.VariantTitle}" DisplayMemberBinding="{Binding Title}" Width="320" />
<GridViewColumn Header="{x:Static loc:Settings.VariantLink}" DisplayMemberBinding="{Binding BookingVariantURL}" Width="315" />
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
and the model like this:
public class BookingVariant
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("booking_variant_url")]
public string BookingVariantURL { get; set; }
}
it is hold in a list:
public ObservableCollection<BookingVariant> BookingVariants { get; private set; }
Now the list binding seems to work, as it fills the correct number of rows. But the columns are not filled. Instead each row just shows the name of the class once. The columns are completely ignored as it seems.
Someone got an idea? Thanks!
Edit:
The Listview currently looks like this:
Upvotes: 0
Views: 1625
Reputation: 310
I found my mistake:
At the top of my window I was importing another XAML file which contains style infos for others controls in that window (list views as well). When removing the file, the Gridview binding works as expected. So my own styles override the behaviour. I will now assign keys to all styles so that they dont get used automatically.
Upvotes: 3
Reputation: 151
It seems you forgot to set the data context. The view control does not know where to look for the binding data.
Set the data context to the object that contains the ObservableCollection.
The source containing class
public class MyData
{
// I dont know your class structure so I added all data in the constructor
public MyData()
{
BookingVariants = new ObservableCollection<BookingVariant>();
this.BookingVariants.Add(new BookingVariant()
{ Title = "Title", BookingVariantURL = "myURL" });
this.BookingVariants.Add(new BookingVariant()
{ Title = "Title2", BookingVariantURL = "myURL2" });
this.BookingVariants.Add(new BookingVariant()
{ Title = "Title3", BookingVariantURL = "myURL3" });
}
public ObservableCollection<BookingVariant> BookingVariants
{
get;
private set;
}
}
In the calling class you define the data context like this:
MyData dataObject = new MyData();
BookingVariantsListView.DataContext = dataObject;
I hope this helps.
Upvotes: 0