I have a custom control and I would like to tell every TextBlock inside to use TextBlock.TextTrimming = CharacterEllipsis but I dont want to set that property on each individually. I mean even if later the user defines a ContentTemplate and places it inside my custom control and that ContentTemplate includes some Textblocks, they should automatically have set TextBlock.TextTrimming = CharacterEllipsis.
How do i do that? Any help please?
You could create an attached property with property value inheritance and apply that to your custom control, for example in its constructor. The attached property would copy its value to the target object, whenever the target object is a TextBlock.
Note that there is no need to define this
TextTrimmingattached property in a derived control class. You could also define it in a special helper class, which does not even need to be derived from DependencyObject.The property also work fine with any other control that uses TextBoxes in their visual tree, for example a standard ContentControl: