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
<ListView>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Name}"
Header="Name"/>
</GridView>
</ListView.View>
</ListView>
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
<ListView>
<ListView.View>
<GridView>
<GridViewColumn Header="Image">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image Source="{Binding Image}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
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…
DO NOT USE THEM TOGETHER!
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
Special thanks goes to Sasha (http://sachabarber.net/) for letting me know of the CodeSnippet plugin for Window Live Writer….
Thanks dude
You are welcome. Just sharing the love
Can you explain how to set the Binding and a Converter in source code and not in XAML?
Greetings, Michael
yea sure…
Binding b = new Binding(“property”);
b.Source = mySource;
b.Converter = new MyConvereter();
SetBinding(SomeProperty, b);
something like that…. 🙂
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…
DO NOT USE THEM TOGETHER!”
good point 🙂
Thanks for posting this. I spent way too much time trying to erroneously define both properties prior to reading this.
“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?
If you want full control of the Visuals use the CellTemplate
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..
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 🙂
this articl was a pile of worthless crapola
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.
Can u please tell how to bind an array items
DisplayMemberBinding=”{Binding ArrayName}” here it is collection of items, please help in this
Pingback: Markus Tamm » Blog Archive » Links 24.03.2010
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?
Pingback: Discution autour de la réflexion | Oliv' & Tom