Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody,

I tried to make a converter for a button in order to give it an arbitrary background and, based on that brush (actually, it supports only SolidColorBrushes, LinearGradientBrushes and RadialGradientBrushes).

I used this code:
XML
<Style TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" removed="{TemplateBinding Background}" SnapsToDevicePixels="true" CornerRadius="5">
                        <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{Binding Path=Background, Converter={StaticResource BTHC}}" />
            </Trigger>
        </Style.Triggers>
    </Style>

inside the style definition of the button. The error is in the triggers, but I cannot understand it... I do not know why it does not work and simply does not modify the background in any way...

Could you please help me sorting out this problem?

Thanks a lot!

Lusvardi Gianmarco
Posted

1 solution

Ok, I've solved it by myself:

XML
<style targettype="Button">
    <setter property="Template">
        <setter.value>
            <controltemplate targettype="{x:Type Button}">
                <border x:name="border" borderbrush="{TemplateBinding BorderBrush}" borderthickness="{TemplateBinding BorderThickness}" background="{TemplateBinding Background}" snapstodevicepixels="true" cornerradius="5" tag="{TemplateBinding Background}" xmlns:x="#unknown">
                    <contentpresenter x:name="contentPresenter" focusable="False" horizontalalignment="{TemplateBinding HorizontalContentAlignment}" margin="{TemplateBinding Padding}" recognizesaccesskey="True" snapstodevicepixels="{TemplateBinding SnapsToDevicePixels}" verticalalignment="{TemplateBinding VerticalContentAlignment}" />
                </border>
                <controltemplate.triggers>
                    <trigger property="IsMouseOver" value="True">
                        <setter property="Background" targetname="border" value="{Binding ElementName=border, Path=Tag, Converter={StaticResource BTHC}}" />
                    </trigger>
                    <trigger property="IsPressed" value="True">
                        <setter property="Background" targetname="border" value="{Binding ElementName=border, Path=Tag, Converter={StaticResource BTPC}}" />
                    </trigger>
                    <trigger property="IsEnabled" value="False">
                        <setter property="Background" targetname="border" value="{Binding ElementName=border, Path=Tag, Converter={StaticResource BTDC}}" />
                    </trigger>
                </controltemplate.triggers>
            </controltemplate>
        </setter.value>
    </setter>
</style>
   

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