How to get child element (itemHoverPhoto) from Image.Tooltip - wpf

<Image Stretch="UniformToFill" x:Name="itemPhoto" Width="120" Height="80" Source="/Resource/Image/gbricks.jpg">
<Image.ToolTip>
<ToolTip Placement="Bottom">
<ToolTip.Template>
<ControlTemplate>
<StackPanel Margin="30,0,30,0">
<Path Margin="34,0,0,0" Fill="#e5AAAAAA" Data="M 0 16 L 16 0 L 32 16 Z"/>
<Image x:Name="itemHoverPhoto" Height="500" Width="500" Source="/Resource/Image/gbricks.jpg"/>
</StackPanel>
</ControlTemplate>
</ToolTip.Template>
</ToolTip>
</Image.ToolTip>
</Image>
How to get child element (itemHoverPhoto) from Image.Tooltip

If you put this inside the OnApplyTemplate() of your window:
ToolTip toolTip = this.itemPhoto.ToolTip as ToolTip;
toolTip.ApplyTemplate();
var itemHoverPhoto = toolTip.Template.FindName("itemHoverPhoto", toolTip);
then you have the itemHoverPhoto, and can manipulate it as you wish

Related

Draw a line under button

I am trying to draw a line under the button, well in fact, last element within the button. The button has three element inside in vertical: image, label, and finally the line I am trying to put. Line must be the same widht than button. Below the code that is not working (line does not appear):
<Button Name="btnDelete" HorizontalAlignment="Center" Margin="30,10" Command="{Binding DeleteCommand}">
<Button.Template>
<ControlTemplate>
<StackPanel Orientation="Vertical">
<Image Height="36" Width="36" Stretch="UniformToFill" Source="/MyResources;component/PNG/Delete.png"/>
<Label HorizontalAlignment="Center">Delete</Label>
<Line Stroke="Orange" X1="0" Y1="25" X2="{Binding ElementName=btnDelete, Path=Width}" Y2="25" />
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
If you just want a straight Line then use a Border. If you just need it horizontal then use it like :
<Button Name="btnDelete" HorizontalAlignment="Center" Margin="30,10" Command="{Binding DeleteCommand}">
<Button.Template>
<ControlTemplate>
<Border BorderBrush="Orange" BorderThickness="0 0 0 3">
<StackPanel>
<Image Height="36" Width="36" Stretch="UniformToFill" Source="/MyResources;component/PNG/Delete.png"/>
<Label HorizontalAlignment="Center">Delete</Label>
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
Use Margin and Padding to adjust it - when needed.
Also u should use Paths instead of Image. You can convert them directly in XAML-Paths with Incscape.
Additional Information Path
A Path is a good way to show Icons in XAML. They can look like:
<Path Stroke="Black" Fill="Gray"
Data="M 10,100 C 10,300 300,-200 300,100" />
You can give them different Brushes and they scale very well in the most scenarious. Inkscape has a good feature - to create the Path out of your Image (svg / png / jpg etc).
There are also Icon-Packs that allready have them like font awesome.

Stretch Path to fill container

I have a resource defined like this:
<Canvas x:Key="export"
Width="48"
Height="48">
<Path Fill="{DynamicResource CurrentColor}"
Data="M23,12L19,8V11H10V13H19V16M1,18V6C1,4.89 1.9,4 3,4H15A2,2 0 0,1 17,6V9H15V6H3V18H15V15H17V18A2,2 0 0,1 15,20H3A2,2 0 0,1 1,18Z" />
</Canvas>
And I'm using it like this:
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="10,5,10,10"
Width="Auto"
Height="Auto">
<Button.Template>
<ControlTemplate TargetType="Button">
<Rectangle Width="48"
Height="48"
Fill="{DynamicResource CurrentColor}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill"
Visual="{Binding}" />
</Rectangle.OpacityMask>
</Rectangle>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
As can be seen I'm trying to set the rectangle size to 48x48 but the Path Data coordinates in the resource were defined to draw it as 24x24. No matter what I try I cannot strech the resource to fill the button. How can it be done?
I'm open to change the button implementation if needed. All I need is to be able to show the icon in the button in a certain color.
Remove canvas and use Stretch="Uniform" on the Path. If you want to reduce height/width of the Path, add it into eg. Grid or just set Width and Height of the Path:
<Grid Width="48" Height="48">
<Path Fill="{DynamicResource CurrentColor}"
Stretch="Uniform"
Data="M23,12L19,8V11H10V13H19V16M1,18V6C1,4.89 1.9,4 3,4H15A2,2 0 0,1 17,6V9H15V6H3V18H15V15H17V18A2,2 0 0,1 15,20H3A2,2 0 0,1 1,18Z" />
</Grid>
Try using HorizontalAlignment="Stretch" and VerticalAlignment="Stretch"
But then again if Width and Height are set they take precedence over the Stretch setting. See Microsoft on HorizontalAlignment.

Set the icon color in a MahApp application

I want to set the icon color in a MahApp application, but the brush is not working. In this example the icoun should be white, but still it is black.
<Rectangle Width="20" Height="20">
<Rectangle.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="White" />
</Rectangle.Resources>
<Rectangle.Fill>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cupcake}" />
</Rectangle.Fill>
</Rectangle>
This is the resource icons.xml file in my app.
<Canvas Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" x:Key="appbar_cupcake">
<Path Width="24" Height="25" Canvas.Left="13" Canvas.Top="11" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 32,14C 33.1046,14 34,14.8954 34,16C 34,16.3643 33.9026,16.7058 33.7324,17L 34,17C 35.1046,17 36,17.8954 36,19C 36,20.1046 35.1046,21 34,21L 35,21C 36.1046,21 37,21.8954 37,23C 37,24.1046 36.1046,25 35,25L 15,25C 13.8954,25 13,24.1046 13,23C 13,21.8954 13.8954,21 15,21L 16,21C 14.8954,21 14,20.1046 14,19C 14,17.8954 14.8954,17 16,17L 16.2676,17C 16.0974,16.7058 16,16.3643 16,16C 16,14.8954 16.8954,14 18,14C 19,14 21,12 25,11C 29,14 31,14 32,14 Z M 15,26L 35,26L 32,36L 18,36L 15,26 Z " />
</Canvas>
What I'm doing wrong?
If you want to dynamically set the Fill color, you can do that by setting the Fill property. As you can see, you are already using the Fill property for the VisualBrush. Fortunately you can use the VisualBrush also in the OpacityMask property.
<Rectangle Fill="Black">
<Rectangle.OpacityMask>
<VisualBrush Visual="{StaticResource appbar_cupcake}" Stretch="Fill" />
</Rectangle.OpacityMask>
</Rectangle>
Hope that helps.

Image Rotation inside WP7 Scrollviewer

In an Windows Phone 7 application, I want to apply a 90ยบ rotation to an image when the user clicks a button. I'm doing the following:
<Image Height="369" Name="imageView" Stretch="Uniform" Width="394">
<Image.RenderTransform>
<RotateTransform Angle="0" />
</Image.RenderTransform>
</Image>
At the function:
((RotateTransform)imageView.RenderTransform).Angle += 90;
So far, so good. The problem appears when I place the image within a scrollviewer.
<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="389" HorizontalAlignment="Left" Margin="12,78,0,0" Name="scrollViewer" VerticalAlignment="Top" Width="409">
<Image Height="369" Name="imageView" Stretch="Uniform" Width="394">
<Image.RenderTransform>
<RotateTransform Angle="0" />
</Image.RenderTransform>
</Image>
</ScrollViewer>
In this case I get an error - InvalidCastException was unhandled - on this line of code:
((RotateTransform)imageView.RenderTransform).Angle += 90;
The problem is that imageView.RenderTransform is not a RotateTransform then, but of CompositeTransform instead, so try this instead:
((CompositeTransform)imageView.RenderTransform).Rotation += 90;
And if you want to specify it as a Composite transform directly in the XAML too, you can do that:
<ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" Height="389" HorizontalAlignment="Left" Margin="12,78,0,0" Name="scrollViewer" VerticalAlignment="Top" Width="409">
<StackPanel>
<Image Height="369" Name="imageView" Stretch="Uniform" Width="394" Source="/PhoneApp2;component/Images/lumia-920-rainbow.png">
<Image.RenderTransform>
<CompositeTransform Rotation="0" CenterX="197" CenterY="184" />
</Image.RenderTransform>
</Image>
<StackPanel>
</ScrollViewer>

Creating a Canvas Background Dynamically

I currently have a canvas which contains a bunch of sqaures as its children.
These sqaures sit on different lines.
I would like to draw a background for the canvas which draws lines (like a notepad would have feint blue lines on the paper)
I would like to draw this dynamically by binding it to a collection of "lines"
So if there are 2 lines in the collection, 2 lines will be drawn on the background of the canvas.
I was looking into using DrawingBrush, but i am not sure if this is the correct way forward
<DrawingBrush>
<DrawingBrush.Drawing>
<Line Name=Line1/>
<Line Name=Line2/>
</DrawingBrush.Drawing>
</DrawingBrush>
(BTW The above code does not work, it is just to explain the conecpt)
Try this approach. Use a new class for your canvas:
internal class SpecialCanvas : Canvas
{
...
ObservableCollection<Line> Lines {get; set;}
DrawingVisual backgroundVisual = new DrawingVisual;
public SpecialCanvas()
{
this.Background = new VisualBrush(backgroundVisual);
}
private void OnLinesChanged(...)
{
using (DrawingContext dc = this.backgroundVisual.RenderOpen())
{
// Draw your lines to dc here.
}
}
}
There are a lot of ways you could possibly do what you want to do. For a simple XAML only solution, you could just use an itemscontrol.
<Window.Resources>
<x:Array x:Key="Lines" Type="{x:Type Line}">
<Line X1="0" X2="400" Y1="25" Y2="25" Stroke="Black" />
<Line X1="0" X2="400" Y1="25" Y2="25" Stroke="Black" />
</x:Array>
</Window.Resources>
<Canvas>
<ItemsControl ItemsSource="{StaticResource Lines}" />
<Rectangle Height="20" Width="20" Canvas.Left="20" Canvas.Top="5" Stroke="Blue" Fill="Blue" />
<Rectangle Height="20" Width="20" Canvas.Left="120" Canvas.Top="5" Stroke="Blue" Fill="Blue" />
<Rectangle Height="20" Width="20" Canvas.Left="20" Canvas.Top="30" Stroke="Blue" Fill="Blue" />
<Rectangle Height="20" Width="20" Canvas.Left="120" Canvas.Top="30" Stroke="Blue" Fill="Blue" />
</Canvas>

Resources