How do you set the Foreground colour for a single Image's ToolTip? (There are multiple Image controls on the page but I want to have the style applied to just one)
Andrew
For the image that you want to set the foreground to, add the following xaml code:
<Image Name="image1" Source="Untitled.png">
<Image.ToolTip>
<ToolTip Foreground="Red">
<StackPanel>
<TextBlock FontWeight="Bold">Submit Request</TextBlock>
<TextBlock>Submits the request to the server.</TextBlock>
</StackPanel>
</ToolTip>
</Image.ToolTip>
</Image>
The Foreground="Red" sets the color of the foreground for that specific tool tip.
OUTPUT:
SOURCE: http://wpftutorial.net/ToolTip.html
Related
I'm using MahApps.Metro for my WPF app. As shown in the image below, the Forground color of a CheckBox remains default (black) if I try to change it to white.
Question: How can we change the content color to white?
None of the following XAMLs change the content's forground color. It remains default (black):
<CheckBox x:Name="chkTest" Content="Test Content" FontSize="20" Foreground="{StaticResource MahApps.Brushes.Badged.Foreground}" />
Or:
<CheckBox x:Name="chkTest" Content="Set Default" FontSize="20" Foreground="White" />
Or -as discussed here:
<CheckBox x:Name="chkTest" Content="Test Content" FontSize="20" Foreground="{StaticResource MahApps.Brushes.CheckBox.ForegroundChecked}" />
Display of the above XAMLs:
The MahApps.Metro CheckBox style has many states and triggers, which use different brushes. Overriding the Forground property does not cover all states.
However, there is a dedicated type CheckBoxHelper with many attached properties that you can use to customize every brush in each state for the content, as well as for the check mark glyph. Look for properties starting with Foreground... and CheckGlyphForeground.... There are attached properties for check mark and content background as well.
<CheckBox x:Name="chkTest" Content="Set Default" FontSize="20"
mah:CheckBoxHelper.ForegroundUnchecked="Blue"
mah:CheckBoxHelper.ForegroundUncheckedMouseOver="Green"
mah:CheckBoxHelper.CheckGlyphForegroundChecked="Red"
mah:CheckBoxHelper.CheckGlyphForegroundCheckedMouseOver="Purple"/>
I have a user control that shows a TextBox along with a small help icon.
My goal is to have a ToolTip pop-up, show some databound text and stay open when the mouse hovers over the help icon.
So, to that end I have created a HelpText dependency property in the user control allowing me to bind a help text string to the user control.
So, my user control looks something like this
<UserControl Name="textField" ...>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding ElementName=textField,Path=Text}"/>
<Image Source="{StaticResource Help.Icon}">
<Image.ToolTip>
<ToolTip Content="{Binding ElementName=textField,Path=HelpText}"/>
</Image.ToolTip>
</Image>
</StackPanel>
</UserControl>
This code does show the tooltip, except that it is empty! Also, the StaysOpen property does not make any difference as the tooltip shuts down after a few seconds.
Funny thing is that when I set the same binding directly on the Image control's ToolTip property the bound text is shown allright in the tooltip pop-up, however it still does not stay open:
<Image Source="{StaticResource Help.Icon}" ToolTip="{Binding ElementName=textField,Path=HelpText}">
So my to questions are:
How come the binding against the user control's HelpText dependency property does not work in the first code sample but does work in the second?
How do I make the ToolTip stay open or rather how do I make the ToolTip both stay open and show the databound text?
Thanks!
ToolTips are not part of the same VisualTree as the rest of your XAML, so the DataContext is not inherited the way you would expect it to be.
Writing ToolTip="{Binding SomeProperty}" will automatically set the ToolTip's DataContext to SomeProperty, however if you build a custom ToolTip you must do this yourself.
<ToolTip DataContext="{Binding PlacementTarget.DataContext,
RelativeSource={RelativeSource Self}}" ... />
This will bind the ToolTip's DataContext to the DataContext of whatever object the ToolTip is on.
To accomplish what you're trying to do, your <ToolTip> would probably look like this, since PlacementTarget would be your Image:
<!-- Could also use something like Tag if DataContext is actually used -->
<Image DataContext="{Binding ElementName=textField, Path=HelpText}"
Source="{StaticResource Help.Icon}">
<Image.ToolTip>
<ToolTip Content="{Binding PlacementTarget.DataContext,
RelativeSource={RelativeSource Self}}"/>
</Image.ToolTip>
</Image>
As for why it won't stay open, I'm not positive but it might be because the ToolTipService.ShowDuration property defaults to 5 seconds, and that probably overwrites the StaysOpen property.
You can try setting it to something higher, such as
<Image ToolTipService.ShowDuration="60000" ... />
Or you can try this workaround of using a Popup styled to look like a ToolTip instead. The code would probably look something like this:
<Popup PlacementTarget="{Binding ElementName=MyImage}"
IsOpen="{Binding IsMouseOver, ElementName=MyImage, Mode=OneWay}">
<TextBlock Text="{Binding ElementName=textField, Path=HelpText}" />
</Popup>
I have a TextBox control and I would like to be able to set a background image and a background color.
Currently I can set one, or the other but not both. When I try to set both simultaneously I receive a "The property 'Background' is set more than once" error.
Here is the code I used:
<TextBox Name="tbImageTextBox">
<TextBox.Background>
<ImageBrush ImageSource="/Resources/Images/image.png"
AlignmentX="Right" Stretch="None"/>
<SolidColorBrush>#FF8D8A8A</SolidColorBrush>
</TextBox.Background>
</TextBox>
I have also attempted to set the background color in the style for the TextBox and the image in the <TextBox.Background>, but the color is ignored.
Use the grid resource for background as needed. Same resource can be used for multiple textboxes.
<Grid>
<Grid.Resources>
<ImageBrush x:Key="img" ImageSource="Blue hills.jpg"></ImageBrush>
<SolidColorBrush x:Key="brownBrush" Color="Brown"></SolidColorBrush>
</Grid.Resources>
<TextBox x:Name="test" Background="{StaticResource img}" Width="100" Height="40" />
</Grid>
I ended up putting the TextBox into a grid with the Background color set and applying the background image to the TextBox itself as using VisualBrush and DrawingBrush stretched my image or only applied the background color to the image - not the rest of the TextBox.
You will need to combine the colour and the image in a single Brush instance, you could use a DrawingBrush or a VisualBrush containing an Image control with your image and the Background set to the colour for example.
You probably want either a VisualBrush or a DrawingBrush. More information on those can be found at MSDN here. Something like this might get you started:
<Rectangle Width="75" Height="75">
<Rectangle.Fill>
<VisualBrush TileMode="Tile">
<VisualBrush.Visual>
<Grid>
<Image BaseUri="somepic.png" />
<Rectangle Brush="FF8D8A8A" />
</Grid>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
how to set balloon tooltip for an image in silverlight?
Use the ToolTipService like so:
<Image>
<ToolTipService.ToolTip>
<ToolTip>
<!-- Any XAML Content Here -->
</ToolTip>
</ToolTipService.ToolTip>
</Image>
How can I set the ChildWindow Title content from a style?
Setting the content to text is straightforward:
Setter Property="Title" Value="My Title Text"
How can I put a StackPanel or Image in the Title from the Setter? I know I could extract the entire style for the control and modify the Chrome but I would like to avoid having all that XAML to wade through just to change a small part.
<basics:ChildWindow.Title>
<StackPanel>
<TextBlock Text="Moo Title"/>
<Rectangle Fill="DarkCyan" Height="25" Width="25"/>
</StackPanel>
</basics:ChildWindow.Title>
Why do you want to use a setter in the first place?