In my View I got a ListView bound to a CollectionView in my ViewModel, for example like this:
<ListView ItemsSource="{Binding MyCollection}" IsSynchronizedWithCurrentItem="true">
<ListView.View>
<GridView>
<GridViewColumn Header="Title" DisplayMemberBinding="{Binding Path=Title}"/>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="Phone" DisplayMemberBinding="{Binding Path=Phone}"/>
<GridViewColumn Header="E-mail" DisplayMemberBinding="{Binding Path=EMail}"/>
</GridView>
</ListView.View>
</ListView>
Right now these GridViewColumns are fixed, but I’d like to be able to change them from the ViewModel. I’d guess I’ll have to bind the GridViewColumn-collection to something in the ViewModel, but what, and how?
The ViewModel does know nothing of WPF, so I got no clue how to achieve this in MVVM.
any help here?
The
Columnsproperty is not a dependency property, so you can’t bind it. However, it might be possible to create an attached property that you could bind to a collection in your ViewModel. This attached property would then create the columns for you.UPDATE
OK, here’s a basic implementation…
Attached properties
ViewModel
XAML :
Note that the
ColumnDescriptorclass is not actually needed, I only added it for clarity, but any type will do (including an anonymous type). You just need to specify which properties hold the header text and display member name.Also, keep in mind that it’s not fully tested yet, so there might be a few problems to fix…