Styling ToolTipService.Tooltip - wpf

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>

Related

WPF Opacity in a general style

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>

Make text bold when mouse hovers over Context menu

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>

Apply style to multiple controls when a style trigger fires in xaml

When the user enters info I want all the controls to look and act the same way, as a result I have done the following.
The label and textbox controls are in a stackpanel as:
<StackPanel Style="{StaticResource ResourceKey=myInput}" HorizontalAlignment="Left">
<Label Content="Label" Name="label1" />
<TextBox Name="textBox1" ></TextBox>
</StackPanel>
the style "myInput" is:
<Style x:Key="myInput" TargetType="{x:Type StackPanel}">
<Style.Resources>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="12" />
</Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="5,-5,2,2"></Setter>
<Setter Property="Height" Value="23"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Background" Value="Blue" >
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Style.Resources>
</Style>
Now every time that I apply that style to a stackpanel that has a label and a textbox the background of the textbox changes to blue when it receives focus.
How could I set the label's fontweight to bold also when that event fires? I will like to do this with xaml.
Use a DataTrigger on your Label which looks to see if the keyboard focus is inside of the parent StackPanel that contains both objects
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="12" />
<Style.Triggers>
<DataTrigger Value="True" Binding="{Binding IsKeyboardFocusWithin,
RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}}">
<Setter Property="FontWeight" Value="Bold" />
</Trigger>
</Style.Triggers>
</Style>

Overriding background color of textbox WPF

I want to change the background of a textbox using a style(in WPF).But text box is already based on a style.So when i give background it setter it is not overridding original style.Below is the code i am trying to work:
<TextBox text="value">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=path}" Value="False"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="ForeGround" Value="Red"/>
<Setter Property="Background" Value="LightGray"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Trigger>
</Style>
The code is chnaging the foreground to Red but there is no chnage in background of textbox.
How can i override the backgorund color.I need to do this in WPF.
This is most definitely not the code you are using, there is no ForeGround property and in theory (that error aside) this would work and the background would change if the trigger is fired.
Another simple example that works:
<CheckBox Name="cb" Content="Red BG"/>
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=cb}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
The following however would not work:
<CheckBox Name="cb" Content="Red BG"/>
<TextBox Background="Green">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=cb}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
Note that the background was set to green first in the declaration, this direct value will override the style due to dependency property value precedence, if you want to set an initial value this needs to be done in a setter in the style (outside of any trigger). I suspect that this might be your issue.
just put the setter statement after the style statement
<Setter Property="Background" Value="red"/>
like..
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Background" Value="red"/>
<Style.Triggers>.........

Setting the text Colour of a WPF DataGrid Row when row is selected

I'm trying to change the colour of Text in the selected row in a WPF datagrid.
By default it changes the text colour white is there a way to change this using styles / triggers etc?
Thanks in advance!
Try this
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" >
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
Then you can use it in the columns that you see fit like
<DataGrid ...>
<DataGrid.Columns>
<DataGridTextColumn CellStyle="{StaticResource DataGridCellStyle}" .../>
If you want it to apply to all columns you can change the x:key of the Style to
<Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}" >
If you want to completely remove the Foreground color changes (say, if your DataGrid has different colors for different rows), you can do this:
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Foreground}" />
</Trigger>
</Style.Triggers>
</Style>
If you want to give this style a name, like in the previous answer, add x:Key.

Resources