Another useful property of the text block control of the WPF is the inline property which is used to Gets an InlineCollection containing the top-level Inline elements that comprise the contents of the TextBlock.A Run is simply a chunk of text with identical formatting. Using a single explicit Run doesn’t add value, but things can start to get interesting when you use multiple Runs in the same TextBlock.
Below is the xaml code which is written and the output of the below code can be seen in the Image 1. In the code snipped for the first text block control I have used simple inline static text to assigned to the text property of the run object. And also set the font style, font weight, foreground color, font size of the run object.So that the each text in single text block control appear differently and you can see this in the Image 1( the first line of the output).
<TextBlock VerticalAlignment="Top" Margin="5,2,0,0" HorizontalAlignment="Left" FontSize="12" > <Run Text="Shane," FontStyle="Italic"/> <Run Text="Bond" FontWeight="Bold" Foreground="Red" FontSize="14"/> <Run Text="(Mr.)"/> </TextBlock > <TextBlock VerticalAlignment="Top" Margin="5,2,0,0" HorizontalAlignment="Left" FontSize="12" > <Run Text="{Binding FirstName}" FontStyle="Italic"/> <Run Text="{Binding LastName}" FontWeight="Bold" Foreground="Red" FontSize="14"/> <Run Text="{Binding Title}" /> </TextBlock > <TextBlock VerticalAlignment="Top" Margin="5,2,0,0" HorizontalAlignment="Left" FontSize="12" > <Run FontStyle="Italic"> Shane, </Run> <Run FontWeight="Bold" Foreground="Red" FontSize="14"> Bond </Run> <Run>(Mr.)</Run> </TextBlock >
The Text property of the Run object is a dependency property, which means that you can bind the Text property to a data source. The Text property fully supports one-way binding in FlowDocumentReader and other controls that display read-only text. For more information about data binding. In the code snipped above I have bind the text property of the run object to the data source. The output of the second text block control can be seen in the Image 1 (second line in the output).
Image 1 |
The last one is the use of the run object without using the text property of the run object. Here you can see that same static text is assigned to the run object as in case of the first text block control. But one important thing is that in the second run object the Bond string is assign to the run object with lot of leading and trailing whitespace is place but the output of the third text block control is same as the output of the first text block control.
Note: When a TextBlock’s content is set via the Text property, any whitespace in the string is preserved. When its content is set via Inlines in XAML, however, whitespace is not preserved. Instead, leading and trailing whitespace is ignored.
Hope you get some idea of how to use the run object and also how to set the property of the individual run object.
All and any comments / bugs / suggestions are welcomed!
1 comment:
Thanks many times.
Post a Comment