How to remove highlight on button if IsMouseHover is true in wpf - wpf

I am developing a WPF project. I want to remove highlight (it is like blue in picture) on button if isMouseHover of Button is true. And I am not sure it is called highlight. Maybe it is probably effect, focus etc.. I added BorderBrush is transparent but it didn't work. The code is as follows:
<Image x:Key="LoginImg" Source="..\Images\Login\oturumac.png"
Stretch="Fill"/>
<Image x:Key="LoginImg_RollOver" Source="..\Images\Login\oturumac_rollover.png"
Stretch="Fill"/>
<Style x:Key="LoginButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Content" Value="{DynamicResource LoginImg_RollOver}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Content" Value="{DynamicResource LoginImg}"/>
</Trigger>
</Style.Triggers>
</Style>
Picture is as follows. First picture when IsMouseOver is true:
How can I solve?
Button code is as follows:
<Button Background="Transparent" BorderBrush="Transparent" Style="{DynamicResource LoginButtonStyle}" Click="btnLogin_Click" HorizontalAlignment="Center" VerticalAlignment="Top" Width="180" Grid.Column="1" Grid.Row="4" x:Name="btnLogin" TabIndex="2"/>

You'll need to provide a new ControlTemplate for the Button to get rid of the default look and feel. You can just replace the default Button ControlTemplate with a plain Image control and replace your Style Triggers with ControlTemplate Triggers. Try this:
<Button>
<Button.Template>
<ControlTemplate>
<Image Name="Image" Stretch="None"
Source="pack://application:,,,/AppName;component/Images/Login/oturumac.png" />
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Image" Property="Source" Value="
pack://application:,,,/AppName;component/Images/Login/oturumac_rollover.png" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>

Related

How to change the orientation property of WrapPanel by trigger?

I want to know how to change the WrapPanel orientation property by triggers.
I use trigger to change the wrap panel orientation property, but it doesn't work.
<WrapPanel Orientation="Horizontal">
<WrapPanel.Style>
<Style TargetType="WrapPanel">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red"/>
<Setter Property="Orientation" Value="Vertical"/>
</Trigger>
</Style.Triggers>
</Style>
</WrapPanel.Style>
<Button Content="button1" Margin="10"/>
<Button Content="button2" Margin="10"/>
</WrapPanel>
I expect the button to be Vertical and the background to be Red, but the result is Horizontal button and Red background. Background is changed right, but orientation is not changed.
set default Orientation value via Style Setter. Orientation="Horizontal" is a local value, which cannot be reset by Style Trigger
<WrapPanel>
<WrapPanel.Style>
<Style TargetType="WrapPanel">
<Setter Property="Orientation" Value="Horizontal"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red"/>
<Setter Property="Orientation" Value="Vertical"/>
</Trigger>
</Style.Triggers>
</Style>
</WrapPanel.Style>
<Button Content="button1" Margin="10"/>
<Button Content="button2" Margin="10"/>
</WrapPanel>

Change style of a child control on MouseOver in WPF

I have a custom control that holds two Rectangles and several TextBoxes. I wish to change the background color of the Rectangle on MouseOver.
I add trigger as following:
<Rectangle
Grid.Column="1"
Fill="#FF383838"
Grid.ColumnSpan="3"
Margin="0,4,4,4">
<Rectangle.Style>
<Style TargetType="{x:Type Rectangle}">
<Setter Property="Fill" Value="#FF383838" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="#FF575757" />
</Trigger>
</Style.Triggers>
</Style>
</Rectangle.Style>
</Rectangle>
But since rectangle is part of my control, I assume the event is not firing.
Setting a property via XAML will be applied over the style properties that you try to set. To fix this, remove Fill=#FF383838 so you should have:
<Rectangle Grid.Column="1"
Grid.ColumnSpan="3"
Margin="0,4,4,4">
//... rest of code here
Try this code:
<Window.Resources>
<Style TargetType="Rectangle" x:Key="test">
<Setter Property="Fill" Value="#FF383838" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" Value="#FF575757" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Rectangle Style="{StaticResource test}" />

How do I create an button that changes the source image AND the color of the text in a StackPanel in WPF?

In my WPF web app I have an image and a textblock inside of a stack panel, making a button. I have it set up so that if I mouseOver the image, it changes the source of the image so that another image appears(same image, but grayed-out). Also, if you mouseOver the textblock the text color changes from black to gray. How do I get both of these things to happen at once if the mouse rolls over either, or over the StackPanel that houses the items? My code:
<StackPanel x:Name="IntegrationRequestLinkStackPanel">
<Button x:Name="IntegrationRequestLinkButton" Content="Integration Request">
<Button.Template>
<ControlTemplate>
<Image>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Propery="Source" Value+"Images/requestNew.png" />
<Setter Property="Cursor" Value="Hand" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="Images"requestNewBW.png" />
</Trigger>
</Style.Trigger>
</Style>
</Image.Style>
</Image>
</ControlTemplate>
</Button>
</Button>
<Textblock Text="Integration Request"............
The code for the Textblock is pretty much the same as for the button. Really I want the whole stack panel to be the button and when mouseOver the image inside changes and the text changes color.
The way you describe it, you want a button with an image and label as its content. You can place both in the template.
<Button x:Name="IntegrationRequestLinkButton"
Content="Integration Request">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<DockPanel LastChildFill="True">
<Image x:Name="Icon"
DockPanel.Dock="Left"
Stretch="None"
Margin="0,0,5,0"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Source="Images/requestNew.png" />
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</DockPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Icon"
Property="Source"
Value="Images/requestNewBW.png" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Typically, one adjusts inherited properties which apply to the entire control (like Foreground) using triggers on the Style as opposed to the Template. Use template triggers when you need to change a specific element defined by your template.
As a matter of style, it seems odd that you would want to show black text and a color image by default, and gray text and a grayscale image on mouse-over. I would expect you to want the opposite.
First off, I'm not sure what you mean by "WPF web app". If you mean Silverlight, I have only tested this code in a desktop application, so your results may differ.
If you want the whole thing (image + text) to be a button, I would say that the best thing is to make the template of the button the StackPanel, rather than the other way around. Here is some code:
<Button>
<Button.Template>
<ControlTemplate TargetType="Button">
<Border Background="Transparent">
<StackPanel>
<Image x:Name="buttonImage"
Width="30" Height="30"
Source="Images/requestNew.png"></Image>
<TextBlock x:Name="buttonText"
Text="Integration Request"></TextBlock>
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="buttonImage"
Property="Source" Value="Images/requestNewBW.png"></Setter>
<Setter TargetName="buttonText"
Property="Foreground" Value="Red"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
So, this is a button with a template containing a StackPanel with a image and a text block which change when you mouse over any part of the button
You'll notice that I added a grid with a transparent background. You can remove it, what it does is it makes the whole rectangular area of the button trigger the change of the IsMouseOver property (if it wasn't there, it would only trigger when the mouse is over the image or the text itself).

WPF: Can I Set IsMouseOver Property of a ChildElement When The Parent Is Moused Over?

Suppose I have a button nested in the Template of a ListBoxItem, can I set the IsMouseOver property of the button to true, so that it looks like its moused over?
Just for illustration, the notifications on the top of the window are what I am referring to. Its basically ListBoxItem's with a TextBlock and Button
Unfortunately, no. "IsMouseOver" is readonly.
I'm assuming, though, that you have a custom control template for the Button, right? If that's the case, one workaround is to mess with the Tag property of the button. Add a trigger to the ControlTemplate that gets fired when a specific Tag value is set. Then, in the DataTemplate for your ListBoxItems, just set the button's Tag to that specific value when IsMouseOver on the item is true. Below is an example:
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<DockPanel x:Name="dp" Background="Transparent">
<Button x:Name="btn" DockPanel.Dock="Right" Content="x" Background="Gainsboro">
<Button.Template>
<ControlTemplate TargetType="Button">
<Border x:Name="bd" Padding="2" BorderBrush="Black" BorderThickness="1"
Background="WhiteSmoke">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bd" Property="Background" Value="LightBlue"/>
</Trigger>
<Trigger Property="Tag" Value="SimulatedMouseOver">
<Setter TargetName="bd" Property="Background" Value="LightBlue"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bd" Property="Background" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
<TextBlock Text="{Binding}"/>
</DockPanel>
<DataTemplate.Triggers>
<Trigger SourceName="dp" Property="IsMouseOver" Value="True">
<Setter TargetName="btn" Property="Tag" Value="SimulatedMouseOver"/>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
<s:String>Item1</s:String>
<s:String>Item2</s:String>
<s:String>Item3</s:String>
</ListBox>

WPF: How to make a "pushlike" checkbox?

I would like to make a CheckBox that looks exactly like a button. My initial feeble attempt doesn't work at all.
<CheckBox x:Name="test">
Testing!
<CheckBox.Template>
<ControlTemplate>
<Button>
<ContentPresenter/>
</Button>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
The ContentPresenter isn't working (the button is empty) and when the button is clicked, the IsChecked property does not toggle. Also, I don't know how to make the button look pushed when IsChecked is true.
Would a ToggleButton suit your needs? CheckBox derives from it, and so they are very similar.
I've just started to write same comment :)
<ToggleButton Name="tb" Height="45" Width="45">
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="Content" Value="False"/>
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
And now as you wanted, Checkbox control:
<CheckBox>
<CheckBox.Template>
<ControlTemplate TargetType="CheckBox">
<ToggleButton x:Name="toggleButton">
</ToggleButton>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True" SourceName="toggleButton">
<Setter Property="Content" Value="True"/>
</Trigger>
<Trigger Property="Content" Value="True">
<Setter Property="IsChecked" Value="True"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
I agree that ToggleButton is the way to go, but if you want your content to show up in your example, try changing your ContentPresenter declaration to this:
<ContentPresenter Content="{TemplateBinding Content}" />

Resources