c# - Run vs. Content vs. Text in WPF -
in wpf (or windows 8 application) have 3 possible ways add text in control.
run
element insidetextblock
element<textblock> <run>my text</run> </textblock>
text
property attribute oftextblock
element<textblock text="my text" />
text
property element insidetextblock
element<textblock> <textblock.text>my text</textblock.text> </textblock>`
what differences between these 3 approaches? , why must use text
textblock
, content
comboboxitem
?
a control text
property can accept string , rendered in specific way handled control. examples of such controls textblock , textbox.
controls content
property can have literally object
set property. these controls forward value content property on contentpresenter. contentpresenter class documentation has relevant block:
the contentpresenter uses following logic display content:
- if contenttemplate property on contentpresenter set, contentpresenter applies datatemplate content property , resulting uielement , child elements, if any, displayed. more information datatemplate objects, see data templating overview.
- if contenttemplateselector property on contentpresenter set, contentpresenter applies appropriate datatemplate content property , resulting uielement , child elements, if any, displayed.
- if there datatemplate associated type of content, contentpresenter applies datatemplate content property , resulting uielement , child elements, if any, displayed.
- if content uielement object, uielement displayed. if uielement has parent, exception occurs.
- if there typeconverter converts type of content uielement, contentpresenter uses typeconverter , resulting uielement displayed.
- if there typeconverter converts type of content string, contentpresenter uses typeconverter , creates textblock contain string. textblock displayed.
- if content xmlelement, value of innertext property displayed in textblock.
- the contentpresenter calls tostring method on content , creates textblock contain string returned tostring. textblock displayed.
in case of textblock class, have option either set text
property, or set inlines
property. setting text
render text. setting inlines
(which default if put content inside body of xaml tag) allows format text. example, use run fontweight set bold
make word or phrase bold within sentence. can use linebreak insert new line. can use inlineuicontainer insert custom ui elements in text. derives inline class can go in collection.
textblock intended simple bits of formatted text. if want more powerful document style features, can flowdocument, used controls such richtextbox, flowdocumentscrollviewer , flowdocumentreader.
as far difference between <textblock text="something" />
, <textblock><textblock.text>something</textblock.text></textblock>
, there isn't difference. 2 different ways can set properties on in xaml file. second version used when need define additional elements inside setter.
Comments
Post a Comment