How to get the instance of the ControlTemplate not the template child instance in Silverlight - silverlight

I want to get the instance of the ControlTemplate through
Validation = GetTemplateChild("ValidationToolTipTemplate") as ControlTemplate;
In Generic.xaml I have this code:
<ControlTemplate x:Key="ValidationToolTipTemplate"> </ControlTemplate>
<Style TargetType="local:DateTimeControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DateTimeControl">
//code goes here
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I can access all the template child that is within the Control template(ControlTemplate TargetType="local:DateTimeControl").
But I also wanted to get the instance of the ControlTemplate x:Key="ValidationToolTipTemplate"
Is it possible to get the instance?

Related

Cannot set unknown member Interaction.Behaviors

I am getting this error when I tried to add behavior to a Framework Element inside a template
Min example:
xmlns:interact="http://schemas.microsoft.com/expression/2010/interactivity"
.....
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<interact:Interaction.Behaviors>
</interact:Interaction.Behaviors>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and when I tried to run the application I am getting
"Cannot set unknown member
'{http://schemas.microsoft.com/expression/2010/interactivity}Interaction.Behaviors'."
Note that this only happens if I define this style inside a resource dictionary. If I cut and paste this style into Window.Resources everything works nicely. How can I fix this?

Using a Button control in a ButtonBase derived template

We have a specific look and feel to buttons in our application and these are defined in a named style for ButtonBase which also happens to be the default style for Button, ToggleButton and RepeatButton.
We also have ToolbarButtons which are derived from ButtonBase and include extra properties such as Text and Icon. These are used to place a specific text and an icon on a ToolbarButton.
The theme for ToolbarButtons is defined as follows in Themes/generic.xaml:
<Style TargetType="{x:Type c:ToolbarButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:ToolbarButton}">
<Button Command="{TemplateBinding Property=Command}">
.. controls to place text and icon etc ..
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
As you can see I'm using a Button control within a control based on ButtonBase and binding the Command to the Command on the contained Button. This hack makes sure that I can override the style of the 'Button' used in the Toolbar by defining the Button default style.
It all seems to work quite well, but it does not feel right using a Button inside a Button. I'm still wondering if I'm doing the right thing. Any ideas?
Your approach is good but you don't have to use a second button in your template, but a ContentPresenter. Use it every time your redefine the template of a ContentControl (a button for example). The BasedOn attribute is useful to override the specific button style.
It looks like this :
<Style TargetType="{x:Type c:ToolbarButton}" BasedOn="{StaticResource {x:Type ButtonBase}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ToolbarButton}">
<StackPanel>
<!-- Image -->
<Image Source="{TemplateBinding Image}"/>
<!-- Content -->
<ContentPresenter Content="{TemplateBinding Content}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You will find the same mechanism with the templates of the ItemsControl (ListBox, ListView, etc) with the ItemsPresenter.
UPDATE:
To take your comment in account, maybe you should override a ContentControl to apply your specific chrome to it:
public class ButtonContentControl : ContentControl { }
<Style TargetType="{x:Type local:ButtonContentControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ButtonContentControl}">
<!-- Specific chrome -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Then you can use it inside the templates of your ButtonBase and your ToolbarButton :
<
Style TargetType="{x:Type ButtonBase}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:ToolbarButton}">
<ButtonContentControl>
<!-- ContentPresenter or something else -->
</ButtonContentControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type c:ToolbarButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type c:ToolbarButton}">
<ButtonContentControl>
.. controls to place text and icon etc ..
</ButtonContentControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Define new ControlTemplate keeping the original events

I want to use the ExtendedWPFToolkits's ColorPicker but with a custom ButtonStyle.
I can create a new look overriding the Template property of the item but the original templates click event is missing.
I want to keep it, but how?
<Controls:ColorPicker >
<Controls:ColorPicker.ButtonStyle>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Button Content="ColorPicker"></Button>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Controls:ColorPicker.ButtonStyle>
</Controls:ColorPicker>
What you have is not valid. You are putting a Button in the ControlTemplate of a ToggleButton, so basically a button in a button.
You'd need to do something like:
<Controls:ColorPicker >
<Controls:ColorPicker.ButtonStyle>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border Background="Transparent">
<TextBlock Text="ColorPicker" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Controls:ColorPicker.ButtonStyle>
</Controls:ColorPicker>
I added a transparent Border so the button will be able to receive mouse events for areas not covered by the text.

Wpf Combobox Rounded Corners

How can i make a combobox edges rounded..?.I have tried in blend,but no success till now..Any input will be highly helpfull
You need to create a custom Style in XAML for your ComboBox in which the outer container is a border with rounded corners. In this particular example, it's a default style that will be blanket applied throughout your application. The content of the control and ContentPresenter must still be declared within the Border.
<Style TargetType="{x:Type ComboBox}">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Border CornerRadius="5">
...
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Silverlight: How to set default style in generic.xaml for child class?

I have the following hierachy:
public class A
{
protected class B
{
}
}
And I've tried to define a default style in the following ways (inside generic.xaml):
<Style TargetType="local:A+B">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:A+B">
<Grid/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:A.B">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:A.B">
<Grid/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:B">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:B">
<Grid/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Does anyone know the correct syntax?
I don't believe inherited styling is supported as it is in CSS. You can only create a style for a specific target type. Then on the instance you need to nominate the style.
However you are using the generic.xaml file (now under themes/generic.xaml) which applies the default style for a specific target type. So if you need to target contained class B you would need to either define the style of B or include it under A through public properties.

Resources