How can I prefix bound values in TextBlock controls in a StackPanel without using separate controls for the prefixes?
E.g., let’s say I have a dialog that uses a TreeView to display a list of books, with the top nodes being the title, and a set of subordinate nodes for the other book attributes (ISBN, Author, etc.).
I have the binding working correctly, but my user wants the book attributes list to stack vertically, and, obviously, he wants each attribute node to have a descriptive prefix before the value (e.g., “Author: Erich Gamma” instead of just “Erich Gamma”). Inside my HDT and DT elements, I am using a StackPanel and TextBlock controls to display the values.
Must I use a separate TextBlock control for the prefix of each attribute
<!-- Works, but requires 2 controls to display the book author and the prefix stacks above the author -->
<TextBlock Text="Author: "/><TextBlock Text="{Binding Path=Author}" />
or is there a way to do this with a single TextBlock control for each node?
<!-- only one control, but doesn't work -->
<TextBlock Text="Author: {Binding Path=Author}" />
I know this must be a common issue, and I Googled for it and searched in the three WPF books I have, but I guess I don’t know the right way to search for what I’m trying to say.
Thanks!
If you have .Net 3.5 SP1, you can easily achive this via StringFormat
You can also do this
If you are not using SP1,
Then you can use a ValueConverter