GridViewColumn DisplayMemberBinding vs CellTemplate

GridViewColumn has 2 different ways how one can specify the content of a cell. One can use the DisplayMemberBinding or also a CellTemplate. DisplayMemberBinding is used when you just want to show some text inside your GridViewColumn. You can use this property as follows

             <GridViewColumn DisplayMemberBinding="{Binding Name}"

So basically all you have to do is specify a binding to the property of the object being bound. In reality the DisplayMemberBinding will create a TextBlock and bind the Text property of the TextBlock to the binding that you specify. Yet if you need more than just text in a TextBlock then DisplayMemberBinding is not what you should be looking for. For such scenario one should use the CellTemplate.

The CellTemplate property of the GridViewColumn allows you to enter a DataTemplate where you can put in whatever you like as controls to be rendered by the GridView. Let me give you an example of how one can use the CellTemplate to do something more than Text

            <GridViewColumn Header="Image">
                        <Image Source="{Binding Image}"/>

So in the above example the ListView is now rendering an image where the source of the image is set via a binding to a property of the object being bound to. This gives you much more flexibility in terms of what you can do inside a Cell. For even more advanced scenarios where you have to decide how to render the cell according to the object being bound you can also use the CellTemplateSelector.

Final and most important point about these 2 properties…

If you set the DisplayMemberBinding it is useless to set the CellTemplate because the GridView will only use the DisplayMemberBinding and ignore the CellTemplate. So always make sure that you only set one of these properties and not both at the same time otherwise you will not get the results that you were hoping for.

Want to read some more on ListView? Click here

17 thoughts on “GridViewColumn DisplayMemberBinding vs CellTemplate

  1. Hey Marlon,
    You saved me some big time today. Suggest you make the following text in your post BOLD and RED :-). Thanks !

    “Final and most important point about these 2 properties…

  2. Thanks for posting this. I spent way too much time trying to erroneously define both properties prior to reading this.

  3. “In reality the DisplayMemberBinding will create a TextBlock and bind the Text property of the TextBlock to the binding that you specify.”

    What does this mean? If so, How can i retrieve the TextBlock?

  4. yes, i know but sometimes a displaymemberbinding
    is simplier; but it’s okay; i did retrieved the
    textblock of the displaymember binding by
    walking in the visual tree; however can’t get the
    adornerlayer if there is..

  5. so you are saying that walking the VirtualTree is simpler thand createing a celltemplate? mmm I would not agree to that.

    You need an AdornerDecorater… Create a CellTemplate that has an AdornerDecorater that contains a textblock and you are done.

    (PS: If you are doing this in code (and there yes I do agree creating a DataTemplate is difficult) you can always create XAML in a string and load it up 🙂

  6. YOU HOMO,

    You know nothing about our projects and you have no right to say that. Digging with DisplayMemberBinding is a more challenge than creating DataTemplate…

    and your comment is worthless crapola.

  7. Can u please tell how to bind an array items

    DisplayMemberBinding=”{Binding ArrayName}” here it is collection of items, please help in this

  8. Pingback: Markus Tamm » Blog Archive » Links 24.03.2010

  9. I would like to HeaderTemplateSelector so that I can change the names of the column headers dynamically whenever the parent object from another grid is selected. I can get this to work at the CellTemplateSelector no problem, but it appears like the HeaderTemplateSelector only works the first time upon form load. Do you know a way to dynamically change the header titles and widths as well?

  10. Pingback: Discution autour de la réflexion | Oliv' & Tom

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s