Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TemplateBinding in wpf style setter?

Tags:

c#

wpf

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?

like image 394
Karthi Keyan Avatar asked Dec 27 '13 10:12

Karthi Keyan


3 Answers

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.

like image 108
gomi42 Avatar answered Oct 07 '22 20:10

gomi42


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.

like image 43
Mark Travis Avatar answered Oct 07 '22 20:10

Mark Travis


You probably need to have Color property defined in template in order to bind to that property.

like image 24
Radenko Zec Avatar answered Oct 07 '22 20:10

Radenko Zec