Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
Can we implement both Datatemplates and controltemplates for a single control(say..Textbox, listbox etc..) for customizing both view and implementing datatriggers for a particular control in WPF..!!!!

If yes, please let me know with an example..

Thanks.
Sekhar.
Posted

1 solution

Implement both Datatemplates and controltemplates for a control doesn't make sense.

You use ControlTemplate to style for a type derived from Control[^] by setting the Template property[^] to that ControlTemplate.

You use DataTemplate to style items in a ListBox, ComboBox, ect.

Both a ControlTemplate and DataTemplate allow you to use Triggers.

I suggest you read this article; WPF Tutorial - Styles, Triggers & Animation[^]
 
Share this answer
 
Comments
Simon Bang Terkildsen 22-Aug-11 7:25am    
Hi Simon. Firstly, I'd like to thank you for the suggested article..that's a quite helpful one, as am new to WPF and trying to learn it..However,when am OP Wrote: trying to implement the same, am getting an error saying..'The resource "MyTextBoxStyle" could not be resolved'. The Xaml code for my form is as follows
<Style TargetType="{x:Type TextBox}" x:Key="MyTextBoxStyle"> </Style> and in my textbox i have used that resource as the following:
<grid>

<textbox x:name="txtValues" height="25" width="80" borderthickness="1" style="{StaticResource MyTextBoxStyle}" xmlns:x="#unknown">
<grid>
So, could you please help me to resolve that error..


When using StaticResource the the resource must be able to be resolved at runtime, meaning the resource has to be defined in your xaml-file or a ResourceDictionary which is merged into your xaml-file.

so this should work for you
<Grid>

<Grid.Resources>

<Style TargetType="{x:Type TextBox}" x:Key="MyTextBoxStyle">

</Style>

</Grid.Resources>

<TextBox x:name="txtValues" height="25" width="80" borderthickness="1" style="{StaticResource MyTextBoxStyle}" xmlns:x="#unknown">
<Grid>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900