Assign binding path at runtime in WPF DataTemplate

Posted by Abiel on Stack Overflow See other posts from Stack Overflow or by Abiel
Published on 2010-06-09T19:06:58Z Indexed on 2010/06/09 19:22 UTC
Read the original article Hit count: 525

Filed under:
|

I am writing a WPF program in C# in which I have a ListView for which the columns will be populated at runtime. I would like to use a custom DataTemplate for the GridViewColumn objects in the ListView.

In the examples I have seen where the number of columns is fixed in advance, a custom DataTemplate is often created using something like the XAML below.

<DataTemplate x:Key="someKey">
    <TextBlock Text="{Binding Path=FirstName}" />
</DataTemplate>

This DataTemplate could also later be assigned to GridViewColumn.CellTemplate in the code-behind by calling FindResource("someKey"). However, this alone is of no use to me, because in this example the Path element is fixed to FirstName. Really I need something where I can set the Path in code.

It is my impression that something along these lines may be possible if XamlReader is used, but I'm not sure how in practice I would do this. Any solutions are greatly appreciated.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about listview