w3arthur
Topic Author
Posts: 4
Joined: 01 Apr 2024, 10:55

Do other components have a Template argument?

13 Apr 2024, 19:17

I am tried to set Template to TextBox but I can't find this argument.
when I try to write it, it still not returning a result.
I am searching more methods to avoid code duplication, like Style and Template.
I'm trying to set some text inside some box that with specific design.

Also, will be thankful to get some explanation how to fix this code and use boxes more correctly.
Image

This my attempt about the "grid" element to make it as a template
Image

Thanks for help.
 
User avatar
jinfox
Posts: 15
Joined: 06 Apr 2019, 00:11

Re: Do other components have a Template argument?

15 Apr 2024, 10:11

Several things can be done to avoid duplication.
First of all, in your example, BasedOn is a property that can be assigned to Styles, but not to Control template
<Style TargetType="{x:Type Grid}">
	<!-- This need to exist somewhere -->
</Style>
<Style x:Key="FooBar.Grid" TargetType="{x:Type Grid}" BasedOn="{StaticResource {x:Type Grid}}">
	<!-- Write your overrides here -->
</Style>
For your usecase, maybe a good approach would be to pack your set of data (text + visibility) inside a Type and then define a DataTemplate for it.
<DataTemplate  x:Key="yourDataTemplate" DataType="{x:Type SomeDataClass}">
	<Border Visibility="{Binding TheVisibility}">
		<TextBlock Text="{Binding PropertyText}" Foreground="{Binding ExampleImportanceColor}"/>
	</Border>
</DataTemplate>
This then allows your to simply bind this data class and have this template replicated to show the data appropriatedly, you can set this datatemplate in a style using the ContentTemplate property.
 
User avatar
sfernandez
Site Admin
Posts: 3001
Joined: 22 Dec 2011, 19:20

Re: Do other components have a Template argument?

19 Apr 2024, 17:24

All FrameworkElements expose a Style property which will allow you to define property setters (and even triggers) to avoid duplication.

Then, all Controls expose also a Template property (of type ControlTemplate) where you can define the appearance of that control and how it reacts to the changes in the control properties (for example, in a Button template you can change the background color of a Border included in the template depending on the values of IsMouseOver or IsPressed properties).

Please explore the hierarchies in the documentation to know all the Control classes available by default.

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 3 guests