W
W
wkololo_4ever2014-01-06 19:00:53
WPF
wkololo_4ever, 2014-01-06 19:00:53

Trigger for color change

There are 2 buttons

<Button Content="Какая-то кнопка" Style="{StaticResource LineButton}"/>
        <Button Content="Button"  Grid.Row="1" Style="{StaticResource LineButton}"/>

Which template I have overridden
<Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <DockPanel LastChildFill="True">
                            <Grid x:Name="controlLayout">
                                <Rectangle x:Name="buttonSurface" 
                                           Fill="{TemplateBinding Background}"/>
                                <Label x:Name="buttonCaption"
                                       Content="{TemplateBinding Content}" 
                                       HorizontalAlignment="Center"
                                       VerticalAlignment="Center"
                                       Background="{TemplateBinding Background}"/>
                            </Grid>
                        </DockPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="buttonCaption"
                                        Property="Background"  Value="#FFFFF500"/>
                                <Setter TargetName="buttonSurface"
                                        Property="Fill" Value="#FFFFF500"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

How can I make it possible to change the color, which in this case is hard-coded "#FFFFF500", in the "IsMouseOver" trigger? That is, so that when creating a new button in xaml, I could determine what color the button background should change to.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ad1Dima, 2014-01-07
@wkololo_4ever

If options with multiple styles are not suitable, then inherit from the button and create a DependencyProperty to which you will bind from the style.

M
Mikhail Andreev, 2014-01-12
@Mikant

I think that the easiest way, in order not to change the existing code, is to make all changes directly in the code (C #) - for example, in the OnApplyTemplate method or in any other initializer, explicitly indicate which button should have which color. those. create some method SetButtonOnMouseOberTriggerColor in which, go through this pattern from the code...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question