Forums

Mouse Hover and Focus style
Last Post 26 May 2016 05:47 PM by Filip Dušek. 5 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
NearUser is Offline
New Member
New Member
Posts:8


--
24 May 2016 08:48 PM
    Hello,
    I have a blue button and I wan't to be red when the mouse is hover and yellow when it get focus. I try this:

    <Button Content="Enter" Margin="10" Height="35" Background="Blue" Command="{Binding ConnectCmd}">
    <Button.Style>
    <Style TargetType="Button">
    <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
    <Setter Property="Background" Value="Red" />
    </Trigger>
    </Style.Triggers>
    </Style>
    </Button.Style>
    </Button>

    But it's not working. Please help
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:676


    --
    25 May 2016 02:10 PM
    You can change default keys for diff color. Use this in some root dictionary.

    <SolidColorBrush x:Key="ButtonBackgroundHoverBrush" Color="Red" />
    <SolidColorBrush x:Key="ButtonBackgroundFocusedBrush" Color="Green" />

    If you want custom style on just one button, you can use BasedOn in some cases or make your own Style with control template.
    NearUser is Offline
    New Member
    New Member
    Posts:8


    --
    26 May 2016 09:57 AM
    Thanks !
    So i wan't to create my own control template, did you write a example or a tutorial ?
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:676


    --
    26 May 2016 10:01 AM
    You can check http://emptykeys.com/Community/aft/65 styles here or any button style for WPF will do. UI Generator will warn you if you use something which is not supported.
    NearUser is Offline
    New Member
    New Member
    Posts:8


    --
    26 May 2016 10:42 AM
    I try this:
     
    <Style x:Key="myButton" BasedOn="{StaticResource {x:Type Button}}" TargetType="Button">
            <Setter Property="Background" Value="MediumSlateBlue" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border SnapsToDevicePixels="True" Padding="5">
                            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsPressed" Value="true">
                                <Setter Property="Background" Value="MediumSlateBlue" />
                            </Trigger>
                            <Trigger Property="IsFocused" Value="true">
                                <Setter Property="Background" Value="RoyalBlue" />
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="Background" Value="PowderBlue" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    


    <Button Content="Play" Style="{DynamicResource myButton}" Margin="10" Height="35" Command="{Binding ConnectCmd}"/>
    



    But it's not working, it show me only the text of the button. (The command "ConnectCmd" works on click)
    Filip DušekUser is Offline
    Advanced Member
    Advanced Member
    Posts:676


    --
    26 May 2016 05:47 PM
    Don't use BasedOn if you have style with everything and you have to use Background="{TemplateBinding Background}" on Border, like its in example, which I posted.

    If you don't know XAML/WPF/Styles etc. you should probably start with something easier tho.
    You are not authorized to post a reply.