Tooltip Placement - WPF - wpf

I'm trying to put together a tool tip for a simple button. However when the mouse is hovered over the button, the tool tip does not appear below it.
Please see this :
This xaml is as below:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<Page.Resources>
<Style x:Key="ToolTipStyle" TargetType="{x:Type ToolTip}">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToolTip">
<Grid x:Name="PopupGrid">
<Grid x:Name="ShadowBackground" Height="65" Width="260">
<Grid.Effect>
<DropShadowEffect BlurRadius="7" ShadowDepth="1" Opacity="0.5" />
</Grid.Effect>
<Path Margin="0 0 50 0" Width="20" Height="10" HorizontalAlignment="Right" VerticalAlignment="Top" Data="M0,10 L10,0 20,10Z" Stroke="Gray" Fill="#EFEFF0" Stretch="None" />
<Border BorderThickness="1 0 1 1" CornerRadius="3" Margin="10 9 10 10" BorderBrush="Gray" Background="#EFEFF0">
<ContentPresenter/>
</Border>
<Border BorderThickness="0 1 0 0" CornerRadius="0 0 3 0" Margin="0 9 10 0" HorizontalAlignment="Right" VerticalAlignment="Top" Width="41" Height="10" BorderBrush="Gray" />
<Border BorderThickness="0 1 0 0" CornerRadius="3 0 0 0" Margin="10 9 69 0" VerticalAlignment="Top" Height="10" BorderBrush="Gray" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="ToolTipHeaderStyle" TargetType="{x:Type Label}">
<Setter Property="FontFamily" Value="Calibri"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontSize" Value="14"/>
</Style>
<Style x:Key="ToolTipTextStyle" TargetType="{x:Type Label}">
<Setter Property="FontFamily" Value="Calibri"/>
<Setter Property="FontSize" Value="12"/>
</Style>
</Page.Resources>
<Grid x:Name="PopupGrid" Background="Red">
<Button Width="100" Height="30" Content="Click Me!">
<Button.ToolTip>
<ToolTip Style="{StaticResource ToolTipStyle}">
<StackPanel Orientation="Vertical">
<Label Content="Newly Rejected" Style="{StaticResource ToolTipHeaderStyle}"></Label>
<Label Content="Please perform requested edits and resubmit" Style="{StaticResource ToolTipTextStyle}"></Label>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
</Grid>
</Page>
I'm not sure what is causing this behavior. Can you please help to get the placement correct?
Forgot to mention how it should appear:
the triangle of the tooltip should be places right below the mouse cursor, which would mean that the tooltip should move towards left.Something like this:
Thanks,
-Mike

Have you played around with the placement properties?
You can add this to your ToolTipStyle:
<Setter Property="ToolTipService.Placement" Value="Left" />
There's also ToolTipService.PlacementRectangle and ToolTipService.PlacementTarget
EDIT:
You could try:
<Setter Property="ToolTipService.HorizontalOffset" Value="-200" />

Use the CustomPopupPlacementCallback.. In my case I needed the tip to display on the right of a textbox, Placement.Right worked fine on my laptop, but was displaying to the left on my touchscreen, the easiest way to fix this is to use the callback to calculate a relative offset in the code behind:
...
tip.PlacementTarget = this;
tip.Placement = PlacementMode.Custom;
tip.CustomPopupPlacementCallback = new CustomPopupPlacementCallback(PositionTooltip);
...
private CustomPopupPlacement[] PositionTooltip(Size popupSize, Size targetSize, Point offset)
{
double offsetY = targetSize.Height / 2 + popupSize.Height;
double offsetX = targetSize.Width;
return new CustomPopupPlacement[] { new CustomPopupPlacement(new Point(offsetX, offsetY), PopupPrimaryAxis.None) };
}

Related

Set margin between legend items

Anybody use RadLegend?
I want the vertical distance between the legend items is 10 px(maybe margin.bottom).
You could define an implicit LegendItemControl style:
<telerik:RadLegend Background="White"
BorderBrush="Black"
BorderThickness="1"
Items="{Binding LegendItems, ElementName=chart1}"
HorizontalAlignment="Right"
VerticalAlignment="Top">
<telerik:RadLegend.Resources>
<Style TargetType="telerik:LegendItemControl" BasedOn="{StaticResource {x:Type telerik:LegendItemControl}}">
<Setter Property="Margin" Value="0 0 0 100" />
</Style>
</telerik:RadLegend.Resources>
</telerik:RadLegend>

Monospace Margin between elements in WPF

I want the space between the child elements in, for example, StackPanel be the same. When using the same Margin for child elements, gap between neighbors doubles. I'm using a little trick to solve this, but it seems to me there is more elegant solution. May be you have one?
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="MyButtonStyle" TargetType="Button">
<Setter Property="Margin" Value="4,4,0,4" />
...
</Style>
<Style x:Key="LastMyButtonStyle" TargetType="Button" BasedOn="{StaticResource MyButton}">
<Setter Property="Margin" Value="4" />
</Style>
I'm using MyButtonStyle for all buttons except the last one, which use LastMyButtonStyle.
Put the StackPanel in another container, i.e. a Border, and set its Margin to the same value as those of the Buttons:
<Border>
<StackPanel Orientation="Horizontal" Margin="2">
<Button Margin="2" Content="Button 1"/>
<Button Margin="2" Content="Button 2"/>
<Button Margin="2" Content="Button 3"/>
</StackPanel>
</Border>

Style RadSlider Ticks

Tererik's documentation is deplorable (or I can't find it). Anyway, anyone have a tip on styling the tick marks for a RadSlider?
I've managed to find this much, that I need to create a RadBarStyle but setting the Foreground doesn't change the ticks, setting the Background does change the bar's background, but has no effect on the ticks.
My code:
<StackPanel Grid.Column="1">
<StackPanel.Resources>
<Style x:Key="ticks" TargetType="{x:Type telerik:RadTickBar}">
<Setter Property="Foreground" Value="White" />
</Style>
</StackPanel.Resources>
<TextBlock Text="Brightness" Margin="5 5 0 10" />
<telerik:RadSlider ValueChanged="RadSlider_ValueChanged"
Ticks="0"
TickPlacement="Both"
Minimum="-100" Maximum="200" LargeChange="10"
SmallChange="5"
TickBarStyle="{StaticResource ticks}"
/>
</StackPanel>
The Tick is a rectangle, if you just want to change it's color you can use something like this:
<StackPanel Grid.Column="1">
<StackPanel.Resources>
<Style x:Key="ticks" TargetType="{x:Type telerik:RadTickBar}">
<Setter Property="Foreground" Value="White" />
</Style>
</StackPanel.Resources>
<TextBlock Margin="5 5 0 10" Text="Brightness" />
<telerik:RadSlider LargeChange="10"
Maximum="200"
Minimum="-100"
SmallChange="5"
TickBarStyle="{StaticResource ticks}"
TickPlacement="Both"
Ticks="0"
ValueChanged="RadSlider_ValueChanged">
<telerik:RadSlider.TickTemplate>
<DataTemplate>
<Rectangle Width="1"
Height="5"
Fill="Red" />
</DataTemplate>
</telerik:RadSlider.TickTemplate>
</telerik:RadSlider>
</StackPanel>

WPF Dynamically add buttons with different multiple text

I create a template for buttons:
<Style x:Key="TableButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Background" Value="{StaticResource GrayBlueGradientBrush}" />
<Setter Property="Width" Value="100" />
<Setter Property="Height" Value="100" />
<Setter Property="Margin" Value="20" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}" ClipToBounds="True">
<!-- Inner Rectangle with rounded corners. -->
<Rectangle x:Name="innerRectangle"
Stroke="Transparent"
StrokeThickness="20"
Fill="{TemplateBinding Background}"
RadiusX="20" RadiusY="20" />
<Ellipse x:Name="NumberGuests"
Width="25"
Height="25"
Stretch="Fill"
StrokeLineJoin="Round"
Fill="Red"
Margin="-70,-70,0,0"/>
<Ellipse x:Name="NumberChecks"
Width="25"
Height="25"
Stretch="Fill"
StrokeLineJoin="Round"
Fill="Green"
Margin="70,-70,0,0"/>
<Rectangle x:Name="Time"
Width="70"
Height="25"
Fill="White"
RadiusX="10" RadiusY="10"
Margin="0,50,0,0"/>
<TextBlock x:Name='TableID'
FontSize="12"
HorizontalAlignment="Center"
FontWeight="Bold"
Margin="0,25,0,0"
Text="Table_New"/>
<TextBlock x:Name='TimeID'
FontSize="12"
HorizontalAlignment="Center"
Margin="0,65,0,0"
Text="Time_New" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Opacity" Value="0.5" />
</Trigger>
</Style.Triggers>
</Style>
Private Sub CreateButton_Click(sender As Object, e As RoutedEventArgs)
Dim TableName As String
Dim TimeNotAvailable As String
LayoutGrid.AllowDragging = True
ToggleButton.Content = "Edit"
Dim LocationButton As New Button
Dim style As Style = TryCast(Me.FindResource("TableButtonStyle"), Style)
TableName = "Test" & I
TimeNotAvailable = "Time" & I
I += 1
LocationButton.Style = style
TableID.Content = TableName
TimeID.Content = TimeNotAvailable
LayoutGrid.Children.Add(LocationButton)
AddHandler LocationButton.Click, AddressOf LocationButtonClicked
End Sub
Every time a "Create Button" button is pressed, a button will be generated based on the template.
However I am not able to set the textblock.text TableID and TimeID.
When creating the buttons I need the tableID and TimeID to get the value of a variable. Like "Table 1", "Table 2" etc.
I tried all different type of bindings, resources etc but binding would change the text in all buttons, first button text would be "Table 1" but when generating the second both buttons would be "Table 2". I also tried DataContext but the both textblocks would have the same data.
I tried creating a button directly in xaml and "TableID.Content = TableName" worked but when using the template "TableID" is not recognized.
Please some assistance. Thanks
As a quick solution you can try this workaround
Add binding to the Text property as follows
<TextBlock x:Name='TableID'
FontSize="12"
HorizontalAlignment="Center"
FontWeight="Bold"
Margin="0,25,0,0"
Text="{Binding [0], FallbackValue=Table_New}"/>
<TextBlock x:Name='TimeID'
FontSize="12"
HorizontalAlignment="Center"
Margin="0,65,0,0"
Text="{Binding [1], FallbackValue=Time_New}" />
and replace following
TableID.Content = TableName
TimeID.Content = TimeNotAvailable
with
LocationButton.DataContext = { TableName, TimeNotAvailable }
The above example demonstrates MVVM way of displaying data on the UI.
Explanation
{ TableName, TimeNotAvailable } creates and an implicit array which passed as DataContext to the button then it is used in binding with indexers where [0] is first element and [1] being the second.

WPF, dynamic Color Fill Rectangle

I try to create a ControlTemplate for my Label like this :
<Style TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle Height="30" HorizontalAlignment="Left" Margin="10"
Stroke="transparent"
VerticalAlignment="Top" Width="3"
Fill="#FF259FED" />
<ContentPresenter HorizontalAlignment="Left" Margin="17,0,0,0" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#7A7E81" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
I want to fill color of rectangle when i create my control, like this for example :
<Label Content="Prénom" VerticalAlignment="Top" CouleurRectangle="#FF259FED" />
So, how can I change Property "Fill" in my controlTemplate to set dynamicly color of rectangle when i create control ?
thank's a lot.
Edit : this is the solution , i create a new class who inherit from Label like this :
Public Class LblTitreChamp
Inherits Label
Public Shared ReadOnly CouleurProperty As DependencyProperty =
DependencyProperty.Register("CouleurRectangle", GetType(SolidColorBrush), GetType(LblTitreChamp))
''' <summary>Propriété pour insérer une couleur au début du Label</summary>
Public Property CouleurRectangle As SolidColorBrush
Get
Return GetValue(CouleurProperty)
End Get
Set(ByVal value As SolidColorBrush)
SetValue(CouleurProperty, value)
End Set
End Property
End Class
then, in my COntrolTemplate :
<Style TargetType="{x:Type local:LblTitreChamp}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:LblTitreChamp}">
<Grid x:Name="LayoutRoot" Background="White">
<Rectangle Height="30" HorizontalAlignment="Left" Margin="10"
Stroke="transparent"
VerticalAlignment="Top" Width="3"
Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Label}}, Path=CouleurRectangle}"/>
<ContentPresenter HorizontalAlignment="Left" Margin="17,0,0,0" RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="#7A7E81" />
<Setter Property="FontWeight" Value="Bold" />
</Style>
and finally, to creat a new label :
<my:LblTitreChamp Content="ID" VerticalAlignment="Top" CouleurRectangle="Black" />
thank's a lot for you :)
hi set Fill={TemplateBinding CouleurRectangle} .Hope this will help. And i Expect that you have created a custom Label class That inherit from Label and has DependencyProperty CouleurRectangle.
Since your label is not a CustomControl, then you cannot dynamically make a property on that so the best possible way is to use the background property of label
...
<Rectangle Height="30" ...
Fill="{TemplateBinding Background}" />
...
Else create a CustomControl inheriting the Label and make a new dependency property and also define a XAML template style for it.

Resources