unruledboy
unruledboy

Reputation: 2333

how to dynamically create controls for GridView DataTemplate in WPF?

I have a GridView control:

       <GridView>
            <GridViewColumn Header="Name" Width="500">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">

                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn DisplayMemberBinding="{Binding TypeName}" Header="Type" Width="100" />
        </GridView>

for the first column("name"), I would like to have a mechanism that when every record is binded, will be a way(e.g. event), so that I can dynamically add controls to the StackPanel.

for example, my data has a column called AnimalType, if it is a cat, I will add an image to the StackPanel; if it is a cow, I will put a media element to play a movie; if it is a dog, I will put a hyper link, etc.

How can I do that?

Upvotes: 0

Views: 2357

Answers (1)

silverfighter
silverfighter

Reputation: 6882

Take a look at the DataTemplateSelector class.... like here: http://www.wpftutorial.net/DataTemplates.html

Upvotes: 1

Related Questions