I'm using <setter>
in my wpf application and i need to use TemplateBinding for that setter Property to evaluate that value at compile time but i can't use TemplateBinding,its throwing an error,
My Code Is Below:
<ControlTemplate TargetType="{x:Type srcview:ButtonView}">
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{TemplateBinding Color}"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</ControlTemplate>
How can i use TemplateBinding
in my style setter or Is there any other way to evaluate the value at compile time?
Indeed setters do not support TemplateBinding. Try this instead:
<Setter Property="Background"
Value="{Binding Color, RelativeSource={RelativeSource Self}}"/>
But be aware the color property you are refering to must be of type brush. Background is a brush and you cannot bind a color to a brush.
If you are desparate to use TemplateBinding, then reverse the role your trigger is playing. Have it set it's values based on IsMouseOver being False, and then use TemplateBinding directly on the button. The downside with this approach is you will have to supply static values in the trigger. For example.
<Window x:Class="StackOverflow._20799186.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ControlTemplate x:Key="ControlAsButtonTemplate" TargetType="{x:Type ContentControl}">
<Button x:Name="MyButton" Content="Hello World!" Background="{TemplateBinding Background}" />
<ControlTemplate.Triggers>
<Trigger SourceName="MyButton" Property="IsMouseOver" Value="False">
<Setter TargetName="MyButton" Property="Background" Value="Silver" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<ContentControl Template="{StaticResource ControlAsButtonTemplate}" Background="Green" />
</Window>
Note the user of the ControlTemplate.Triggers rather than the Button.Style.Triggers.
I hope this helps.
You probably need to have Color property defined in template in order to bind to that property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With