I am wondering how to change the text when the mouse hovers over text in a context menu? I want the text to become Bold but not sure how to do this. Any help would be appreciated :). This is my resource style for the menu Items containing an image, which is where I think I should put it
XAML
<Style x:Key="MenuItemIcon" TargetType="MenuItem">
<Style.Resources>
<Style TargetType="ContentPresenter">
<Style.Triggers>
<Trigger Property="ContentSource" Value="Icon">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="{Binding}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
You can try following :
<Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}" x:Shared="False">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
Let me know if it works for you :)
Regards,
To answer your another question, to change hover background please use the following :
<Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}" x:Shared="False">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
</Style.Triggers>
Related
I have this style that works as expected. I'm trying to generalize it for all controls.
Issue: if I replace the type ComboBox with Control. It does not work anymore.
I'm trying to avoid creating a style for each type of control.
<Style TargetType="{x:Type ComboBox}">
<Setter Property="IsEnabled" Value="{Binding Path=myProperty}"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="ComboBox.Opacity" Value="1" />
</Trigger>
</Style.Triggers>
</Style>
I don't think there's a way to do exactly what you want. While this won't allow you to avoid defining styles for each type it does cut down on the duplicated code by using BasedOn to inherit the style you define once:
<Resources>
<Style x:Key="InvisibleWhenDisabled" TargetType="{x:Type Control}">
<Setter Property="IsEnabled" Value="{Binding Path=myProperty}"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="ComboBox" BasedOn="{StaticResource InvisibleWhenDisabled}"/>
<Style TargetType="Button" BasedOn="{StaticResource InvisibleWhenDisabled}"/>
</Resources>
I am dimming the Opacity on the custom control items in a ListView and would like to have them set back to full opacity OnMouseOver. I can dim them OnMouseOver, but I can not increase the Opacity. I am guessing it has something to do with the ListView ControlTemplate. Any help is appreciated it is getting very frustrating:
<ListView Background="Transparent" BorderBrush="Transparent" Grid.Row="0" VerticalAlignment="Top">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value=".9"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
<notify:NotificationInfo/>
</ListView>
I don't know if this is useful:
<ListView>
<ListViewItem>Johan</ListViewItem>
<ListViewItem>Ray</ListViewItem>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value=".9"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Opacity" Value=".2"/>
</Trigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
Did not feel super nice.
I have a WPF app with a ComboBox where I want to style the items but get an unexpected behaviour depending upon which item is selected.
The following XAML snippet demonstrates the problem:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Page.Resources>
<Style x:Key="ItemStyle" TargetType="{x:Type ComboBoxItem}">
<Style.Triggers>
<Trigger Property="Content" Value="ABC">
<Setter Property="FontStyle" Value="Oblique"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="BoxStyle" TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="Text" Value="ABC">
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Border Width="200">
<ComboBox Style="{StaticResource BoxStyle}"
ItemContainerStyle="{StaticResource ItemStyle}"
Height="20">
<ComboBoxItem>ABC</ComboBoxItem>
<ComboBoxItem>DEF</ComboBoxItem>
<ComboBoxItem>GHI</ComboBoxItem>
</ComboBox>
</Border>
</Page>
This displays a simple ComboBox with three items; ABC, DEF and GHI. Note that ABC is shown in the dropdown with oblique, red text and also in the selection box when selected.
However, if I then open the dropdown again all 3 items are shown oblique and red.
If the DEF or GHI items are selected then these show in normal font, black and on opening the dropdown again show correctly - with ABC still showing oblique, red.
What am I doing wrong?
This is because of Dependency Property Value Precedence. When ABC is selected the ComboBoxItems in the DropDown inherit the FontStyle and Foreground from the ComboBox.
This will fix the code as the ComboBoxItems won't inherit FontStyle and Foreground from the ComboBox:
<Page.Resources>
<Style x:Key="ItemStyle"
TargetType="{x:Type ComboBoxItem}">
<Setter Property="FontStyle"
Value="Normal" />
<Setter Property="Foreground"
Value="Black" />
<Style.Triggers>
<Trigger Property="Content"
Value="ABC">
<Setter Property="FontStyle"
Value="Oblique" />
<Setter Property="Foreground"
Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="BoxStyle"
TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="Text"
Value="ABC">
<Setter Property="FontStyle"
Value="Italic" />
<Setter Property="Foreground"
Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Border Width="200">
<ComboBox Style="{StaticResource BoxStyle}"
ItemContainerStyle="{StaticResource ItemStyle}"
Height="20">
<ComboBoxItem>ABC</ComboBoxItem>
<ComboBoxItem>DEF</ComboBoxItem>
<ComboBoxItem>GHI</ComboBoxItem>
</ComboBox>
</Border>
When I do mouse over on column header of datagrid, its close button should popup with header to remove column.
I have below XAML to capture mouseover of header.
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Height" Value="26" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource ActiveItemButtonPressedBrush}" />
?????????? what should I write here to create that button ??????????
</Trigger>
</Style.Triggers>
</Style>
Please help me to generate XAML as I am new to WPF.
you need to override DataGrid Column's HeaderTemplate as a
....
<DataTemplate>
<Button>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
... do you contents here
..text block as visible
<Button x:Name="btn"..round button as collapsed
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource ActiveItemButtonPressedBrush}" />
<Setter TargetName="btn" Property="Visibility" Value="Visible"/>
</Trigger>
</Setter>
Is there a way to apply a style just to some tooltips?
I'm trying to specify tooltip template just for tooltips showing validation erros.
Suppose I have a tooltip style, say errorTTStyle, and some validation template:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
How to force WPF to use errorTTStyle just for this situation (I know I can change tootlip style globaly, but that's not what I want)?
You could add the style for the toolTip in the resources of the Textbox style and it would only be used by the parent style, also base that style in the errorTTStyle if you want to use an external style:
<Style TargetType="{x:Type TextBox}">
<Style.Resources>
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource errorTTStyle}" />
</Style.Resources>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>