I tried to set the Tooltip content property in style. But Tooltip text is displayed as System.Windows.Style. Can someone help me with what I am doing wrong?
<TextBlock HorizontalAlignment="Left" Margin="149,45,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="29" Width="121">
<TextBlock.ToolTip>
<Style TargetType="{x:Type ToolTip}">
<Setter Property="Content" Value="ToolTip Test"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
</Style>
</TextBlock.ToolTip>
</TextBlock>
You have to assign a ToolTip to the TextBox.ToolTip property and then assign the Style to ToolTip.Style:
<TextBlock HorizontalAlignment="Left" Text="TextBlock">
<TextBlock.ToolTip>
<ToolTip>
<ToolTip.Style>
<Style TargetType="{x:Type ToolTip}">
<Setter Property="Content" Value="ToolTip Test" />
<Setter Property="Foreground" Value="Red" />
<Setter Property="Foreground" Value="White" />
</Style>
</ToolTip.Style>
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
When setting FrameworkElement.ToolTip directly, the object is wrapped implicitly into a ToolTip. Since Style isn't a FrameworkElement and cannot be rendered, the ContentControl (ToolTip) invokes object.ToString on the content (the Style in your case) which returns the fully qualified type name as string by default.
Related
I have this textblock with default foreground color is white
<TextBlock Text="First Cmd" Grid.Row="0" TextAlignment="Center" Margin="4" TextWrapping="Wrap" Foreground="White" Style="{DynamicResource ABC}">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding AAA}" MouseAction="LeftClick" />
</TextBlock.InputBindings>
</TextBlock>
When the mouse is over the textblock, the forground color must change in black, but this Style doesn't work. Why ?
<Style x:Key="ABC" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="Black">
</Trigger>
</Style.Triggers>
</Style>
You set the Foreground for TextBlock locally, so the Trigger setter cannot override that. You need to use a Style setter to set the initial Foreground:
<Style x:Key="ABC" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="White"/>
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="Black">
</Trigger>
</Style.Triggers>
</Style>
The Foreground="White" should be removed from the <TextBlock ....
Understand more about Dependency Property Value Precedence.
I have a lot of labels on my wpf App like this.
<Label Style="{StaticResource styleLabelTitle}">
<TextBlock TextTrimming="CharacterEllipsis" Text="{localization:Translate geolocation_controls}">
</TextBlock>
</Label>
I want to add a tooltip to show complete name when ellipsis is working. So i add the tooltip in the label style.
<Style x:Key="styleLabelTitle" TargetType="Label" x:Shared="False">
<Setter Property="Foreground" Value="{StaticResource brushTextsForeground}"></Setter>
<Setter Property="FontWeight" Value="Bold"></Setter>
<Setter Property="FontFamily" Value="Consolas"></Setter>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
</Style>
The problem is that I think when tooltip is appearing is changing the textblock parent. So the text is only appearing in the Tooltip and is removed from the original label.
Any ideas?
Thanks in advance.
I finally came to a solution by doing a new style.
<Style x:Key="styleLabelText" TargetType="{x:Type Label}"
x:Shared="False">
<Setter Property="Foreground" Value="{StaticResource brushTextsForeground}"></Setter>
<Setter Property="FontWeight" Value="Normal"></Setter>
<Setter Property="FontFamily" Value="Consolas"></Setter>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
Text="{TemplateBinding Content}" />
</ControlTemplate>
</Setter.Value>
</Setter> </Style>
You only have an instance of the textblock, then when you assign it to the tooltip the content is lost as you said.
You should set the value of the tooltip with the translated text out of the style:
<Label Style="{StaticResource styleLabelTitle}" ToolTip="{localization:Translate geolocation_controls}">
<TextBlock ...>
</Label>
In my Window.Resources I have the following style:
<Style TargetType="TextBox" x:Key="HintText" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Style.Resources>
<VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
<VisualBrush.Visual>
<Label Content="{DynamicResource EmptyText}" Foreground="LightGray" />
</VisualBrush.Visual>
</VisualBrush>
</Style.Resources>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="Text" Value="{x:Null}">
<Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
</Trigger>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="Background" Value="White" />
</Trigger>
</Style.Triggers>
</Style>
If I use this for 1 TextBox with this,
<Label Content="Test" Foreground="LightGray" />
Test will show up in my TextBox if it's empty. When I try to use this style in different TextBoxes with this,
<Label Content="{DynamicResource EmptyText}" Foreground="LightGray" />
and
<TextBox.Resources>
<sys:String x:Key="EmptyText">Test</sys:String>
</TextBox.Resources>
it doesn't show anything. Is it possible to use this 1 style with a different string that is shown in the TextBox or do I have to make a different style for each TextBox?
You don't appear to be employing this style in any of the examples you give and it isn't at all clear what relationship your last XAML block has with the one before it.
However, yes you should be able to redefine EmptyText as often as you like. The Text property will be resolved in accordance with the Dependency Property value precedence rules.
So you can do something like this:
<DockPanel HorizontalAlignment="Stretch">
<DockPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Text"
Value="{DynamicResource EmptyText/>
</Style>
<sys:String x:Key="EmptyText">Defined in the Dockpanel resource</sys:String>
</DockPanel.Resources>
<TextBlock/>
<TextBlock>
<TextBlock.Resources>
<sys:String x:Key="EmptyText">Defined in the textbox resource</sys:String>
</TextBlock.Resources>
</TextBlock>
<TextBlock>
<TextBlock.Resources>
<sys:String x:Key="EmptyText">Also defined at the textbox</sys:String>
</TextBlock.Resources>
</TextBlock>
</DockPanel>
i have a base style and a style in wpf.
the base style is:
<Style x:Key="BaseTextBox" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="#DDFFDD" />
<Setter Property="MinWidth" Value="75" />
<Setter Property="behaviors:OCCInteraction.Triggers" Value="{StaticResource ResourceKey=validationTrigger}" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="#FFDDDD"/>
<Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" />
</Trigger>
</Style.Triggers>
</Style>
And the the specific style is:
<Style x:Key="EditableTextBox" TargetType="{x:Type TextBox}" BasedOn="{StaticResource ResourceKey=BaseTextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<StackPanel Orientation="Horizontal">
<Border BorderBrush="Red" BorderThickness="1" Padding="0" Margin="0">
<AdornedElementPlaceholder Margin="0"/>
</Border>
<TextBlock Text="test" />
<Image Style="{StaticResource ResourceKey=WarningImage}"/>
<TextBlock Text="{Binding Path=(Validation.Errors)[0].ErrorContent, RelativeSource={RelativeSource Mode=Self,AncestorLevel=2}}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now i want to add the Validation Error Text to a textblock next to the image. But the Same Binding Path doesn't work. I've tried diferent bindings, but I can't figure out how to access the same binding like on the base style.
Thanks for help :)
Have you tried it without the AncestorLevel? You should be the same object.
You cannot use Mode=Self and AncestorLevel properties. Just use Mode=Self.
Ancestor level is used when you try to reach parent of that control in visual tree.
I'm trying to do the following. I have a label bound to an object that have two properties. One I want to display and one I want to use for the datatrigger.
Here what's I've come up with yet :
<Label Grid.Row="5" Content="{Binding ElementName=InformationUserControl, Path=Info.ObjectBound}">
<Label.Style>
<Style TargetType="{x:Type Label}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource self}, Path=Content.InterpretationValue}">
<DataTrigger.Value>
<enums:DataInterpretation>Neutral</enums:DataInterpretation>
</DataTrigger.Value>
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<TextBlock Text="{TemplateBinding Content.Value}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Label.Style>
</Label>
The problem is that my Template overrides the default one so it display nothing. Is there a way to make it work?
Thanks !
I think the problem is not that you override the template but that the binding is broken, i would try this:
<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content.Value}" />