WebView Progress ring windows 8.1 app - c#

I have a problem, I want to use a Progress ring in webview but it does not do what I want it to do. The progress ring stays even if the page is loaded. I also want the buttons appear after loading.
Here's my code:
xaml.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// Die Elementvorlage "Leere Seite" ist unter http://go.microsoft.com/fwlink /?LinkId=234238 dokumentiert.
namespace Euregio_Systems
{
/// <summary>
/// Eine leere Seite, die eigenständig verwendet werden kann oder auf die innerhalb eines Rahmens navigiert werden kann.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.webBrowser.Navigate(new Uri("http://www.euregio-systems.com", UriKind.Absolute));
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.webBrowser.InvokeScript("eval", new[] { "history.go(-1)" });
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
this.webBrowser.Navigate(new Uri("http://www.euregio-systems.com", UriKind.Absolute));
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
this.webBrowser.InvokeScript("eval", new[] { "histroy.go(+1)" });
}
private void webBrowser_NavigationCompleted(object sender, NavigatingCancelEventArgs e)
{
pr1.IsActive = false;
pr1.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
webBrowser.Visibility = Windows.UI.Xaml.Visibility.Visible;
fwd.Visibility = Windows.UI.Xaml.Visibility.Visible;
backButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
homeButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
}
}
}
and xaml
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView Name="webBrowser" Visibility="Collapsed"/>
<AppBarButton Visibility="Collapsed" Name="backButton" HorizontalAlignment="Left" Margin="-20,-4,0,0" VerticalAlignment="Top" Click="Button_Click_1" Icon="Back"/>
<AppBarButton Visibility="Collapsed" Name="homeButton" HorizontalAlignment="Left" Margin="56,-4,0,0" VerticalAlignment="Top" Click="Button_Click_2" Icon="Home"/>
<AppBarButton Visibility="Collapsed" Name="fwd" HorizontalAlignment="Left" Margin="132,-4,0,0" VerticalAlignment="Top" Icon="Forward" Click="Button_Click_3"/>
<ProgressRing Visibility="Visible" Name="pr1" HorizontalAlignment="Center" VerticalAlignment="Center" IsActive="True" Height="163" Width="170" Margin="0" RequestedTheme="Light"/>
</Grid>
</Page>

You have to add this line before calling Navigate on the web browser in your code:
this.webBrowser.NavigationCompleted+= webBrowser_NavigationCompleted;
Also you should ideally do this on loaded event of WebView which can be added like this:
<WebView Name="webBrowser" Visibility="Collapsed" loaded="WebViewControl_Loaded"/>

If you have forgotten to subscribe to the event, you should open the Form/Page designer and check for the "Lightning" Icon to display the events you can subscribe to, like in this image
Just look for the NavigationCompleted event and put your callback method name (webBrowser_NavigationCompleted) in.
Though using the events is a bit discouraged these days. MVVM is the preferable method of developing Windows Store Apps (or any modern WPF Application) to fully utilize WPF's power of binding.
Update:
Or if you prefer it, you can also subscribe in code (just make sure you do it after the Components have been initialized):
this.webBrowser.NavigationCompleted += webBrowser_NavigationCompleted;

Related

Getting image file path from PreviewMouseDown event (WPF/C#)

I'm making an image viewer. I have a mouse event that is supposed to trigger when an image (or child of StackPanel in this case), is clicked. The event is triggered as intended. The problem is, since the pathfile of the images is different depending on whichever is selected (in which directory, etc.), I just don't know how to get the pathfile of the specific image that is currently clicked on. The directory depends on whichever folder is loaded, so the pathfile could be different everytime.
If you scroll all the way down to Image_Load(), I have the Pic.Fill which is a reference to the rectangle that is being filled with the image. I'm trying to find the first parameter for Uri(_,_), which is supposed to be where the #"path" of the current image is.
using System;
using System.IO;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Drawing;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
List<string> paths = new List<string>();
string[] extension = new[] { ".jpg", ".png", ".jpeg", ".gif", ".bmp", ".tif", ".tiff" };
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
FileLocation.Text = fbd.SelectedPath;
}
}
private void OpenButton_Click(object sender, RoutedEventArgs e)
{
LoadPhotos();
}
private void LoadPhotos()
{
try
{
DirectoryInfo di = new DirectoryInfo(fbd.SelectedPath);
foreach (var fi in di.GetFiles().Where(f => extension.Contains(f.Extension.ToLower())).ToArray())
{
paths.Add(fi.FullName);
}
Photos.ItemsSource = paths;
} catch (Exception e)
{
Console.WriteLine("File not found.", e.Source);
}
}
private void Image_Load(object sender, MouseButtonEventArgs e)
{
// What is the reference here?
Pic.Fill = new ImageBrush { ImageSource = new BitmapImage(new Uri(/*Insert pathfile here*/, UriKind.Absolute)) };
}
}
}
Here's my .xaml. I've analyzed it to see a reference but the only thing that stands out is the Image Source, which I've tagged with a comment. It's set to { Binding . } which I'm going to assume means, whatever image is referenced. But how do I connect that variable reference to my Image_Load()? (which is declared on the instantiation of ListBox)
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="1081" Width="1720.5">
<Grid Margin="0,0,2,-0.578">
<Button x:Name="OpenButton" Content="Load" HorizontalAlignment="Left" Height="44" Margin="1166.374,932.042,0,0" VerticalAlignment="Top" Width="145.666" Background="#FFDDDDDD" FontSize="24" Click="OpenButton_Click"/>
<Button x:Name="BrowseButton" Content="Browse" HorizontalAlignment="Left" Height="44" Margin="58.666,932.042,0,0" VerticalAlignment="Top" Width="134.166" Background="#FFDDDDDD" FontSize="24" Click="Button_Click"/>
<Rectangle x:Name="Pic" Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="878.767" Margin="58.666,34.733,0,0" Stroke="Black" VerticalAlignment="Top" Width="1253.374"/>
<ScrollViewer VerticalScrollBarVisibility="Visible" Width="327.46" HorizontalAlignment="Left" Margin="1339.04,34.733,0,137.078" RenderTransformOrigin="0.5,0.5" >
<StackPanel Width="327.46" RenderTransformOrigin="0.516,0.496" HorizontalAlignment="Left" VerticalAlignment="Center" Height="878.085">
<ListBox x:Name="Photos"
Grid.Row="2" Grid.Column="1" Height="876" PreviewMouseDown="Image_Load">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="300" Height="100">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
/* Image Source? */
<Image Source="{Binding .}" Grid.Column="0"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</ScrollViewer>
<TextBlock x:Name="FileLocation" HorizontalAlignment="Left" Height="44" Margin="192.832,948.542,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="973.542" Foreground="#FF515151" TextAlignment="Center" FontSize="14"/>
</Grid>
</Window>
I've been learning on the way using Blend-- some youtube tutorials here and there. I've tried to look for a solution online but all the questions about it usually involve a static and specific directory. This is my first program and the only thing I need is that darn path file reference. There are so many object reference so I just don't even know how to wrap my head around it, being so unfamiliar with these classes. Some assistance would be very much appreciated! :)

C# ProgressBar not showing progress in media player

everyone. I've encountered a problem while developing my test media player in C#. I think I put all the code, but when I open file, it starts playing but progress bar isn't moving. Here is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Pickers;
using System.Threading;
using System.Windows;
namespace App1
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += timer_Tick;
timer.Start();
}
TimeSpan _position;
private async void Open_Click(object sender, RoutedEventArgs e)
{
var openPicker = new FileOpenPicker();
openPicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
openPicker.FileTypeFilter.Add(".wmv");
openPicker.FileTypeFilter.Add(".mp4");
openPicker.FileTypeFilter.Add(".mp3");
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
var stream = await file.OpenAsync(FileAccessMode.Read);
videoMediaElement.SetSource(stream, file.ContentType);
}
}
private void Play_Click(object sender, RoutedEventArgs e)
{
if(videoMediaElement.PlaybackRate != 1 )
{
videoMediaElement.DefaultPlaybackRate = 1;
}
videoMediaElement.Play();
}
private void Stop_Click(object sender, RoutedEventArgs e)
{
videoMediaElement.Stop();
}
private void ProgressBar_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
}
private void timer_Tick(object sender, object e)
{
if (videoMediaElement.Source != null && videoMediaElement.NaturalDuration.HasTimeSpan)
{
ProgressBar.Minimum = 0;
ProgressBar.Maximum = videoMediaElement.NaturalDuration.TimeSpan.TotalSeconds;
ProgressBar.Value = videoMediaElement.Position.TotalSeconds;
}
}
private void videoMediaElement_MediaOpened(object sender, RoutedEventArgs e)
{
_position = videoMediaElement.NaturalDuration.TimeSpan;
ProgressBar.Minimum = 0;
ProgressBar.Maximum = _position.TotalSeconds;
}
}
}
and XAML:
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="Open" Content="Open" HorizontalAlignment="Left" Margin="60,564,0,0" VerticalAlignment="Top" Width="299" Click="Open_Click"/>
<Button x:Name="Play" Content="Play" HorizontalAlignment="Left" Margin="393,564,0,0" VerticalAlignment="Top" Width="299" Click="Play_Click"/>
<Button x:Name="Stop" Content="Stop" HorizontalAlignment="Left" Margin="737,564,0,0" VerticalAlignment="Top" Width="299" Click="Stop_Click"/>
<ProgressBar x:Name="ProgressBar"
HorizontalAlignment="Left" Height="16" Margin="60,665,0,0" VerticalAlignment="Top" Width="1185" ValueChanged="ProgressBar_ValueChanged"/>
<Slider x:Name="slider" HorizontalAlignment="Left" Margin="1082,557,0,0" VerticalAlignment="Top" Width="163"/>
<MediaElement x:Name="videoMediaElement" HorizontalAlignment="Left" Height="491.884" Margin="4.22,58.023,0,0" VerticalAlignment="Top" Width="1270.501" RenderTransformOrigin="0.5,0.5" UseLayoutRounding="False" d:LayoutRounding="Auto" MediaOpened="videoMediaElement_MediaOpened">
<MediaElement.RenderTransform>
<CompositeTransform Rotation="0.111"/>
</MediaElement.RenderTransform>
</MediaElement>
</Grid>
PLZ HELP!! I wasted 2 days and I found no real solution on google. Thank you!
Well I looked at your code and found your problem. It appears that SetSource() method doesn't really set mediaelement's source. I have no idea why. Maybe someone here will help you with that. There are several things that I can point out.
You start filling this progressbar before the movie starts. Which is a problem. You could initialize your timer not in MainPage constructor but in your Play_Click event or in this case where your movie starts automatically in you Open_Click event.
As I mentioned since the source is always null the progressbar will never fill. Fortunately there is a way to check if video element is currently running. Replace this:
if (videoMediaElement.Source != null && videoMediaElement.NaturalDuration.HasTimeSpan)
With This:
if (videoMediaElement.CurrentState == MediaElementState.Playing)
Hope this helps.

WPF - Webbrowser - getElementById

In a WPF application, I have a webbrowser called WebBrowser1. This refers to an HTML page which contains a TextArea to which users can input text.
<html>
<body>
<textarea class="myStudentInput" id="myStudentInput1">
Text to be copied
</textarea>
</body>
</html>
I wish to get this text and potentially also set this text.
I have tried something similar to the javascript way of writing it:
document.getElementById("myStudentOutput1").innerHTML;
such as
HtmlElement textArea = webBrowser1.Document.All["myStudentInput1"];
dynamic textArea = WebBrowser1.Document.GetElementsByID("myStudentInput1").InnerText;
but it doesn't work.
The following solution in Visual Studio 2015 WPF Application works for me.
First, add a reference to the Microsoft HTML COM Library. This is on the COM tab, when you do an "Add Reference" in your project.
Then add the code:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication3"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<WebBrowser x:Name="WebBrowser1" HorizontalAlignment="Left" Height="480" Margin="10,10,0,0" VerticalAlignment="Top" Width="770" Source="E:\Others\Dropbox\Programming\Questions.html"/>
<Button x:Name="mySetQuestionButton" Content="Set Question" HorizontalAlignment="Left" Margin="200,520,0,0" VerticalAlignment="Top" Width="75" Click="mySetQuestion"/>
<Button x:Name="myGetAnswerButton" Content="Get Answer" HorizontalAlignment="Left" Margin="350,520,0,0" VerticalAlignment="Top" Width="75" Click="myGetAnswer"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="600,520,0,0" TextWrapping="Wrap" Text="Hello2" VerticalAlignment="Top"/>
</Grid>
</Window>
and
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void mySetQuestion(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myQuestion1");
textArea.innerHTML = "What is 1+1?";
}
private void myGetAnswer(object sender, EventArgs e)
{
mshtml.HTMLDocument document = (mshtml.HTMLDocument)WebBrowser1.Document;
mshtml.IHTMLElement textArea = document.getElementById("myStudentInput1");
textBlock.Text = textArea.innerHTML;
}
}
}
but it doesn't work.
I have no idea what that could possibly mean. All you can get is a code snippet that does work:
public partial class Form1 : Form {
private WebBrowser webBrowser1;
private Button button1;
public Form1() {
button1 = new Button { Text = "Test" };
button1.Click += button1_Click;
this.Controls.Add(button1);
webBrowser1 = new WebBrowser { Dock = DockStyle.Fill };
webBrowser1.DocumentText = #"<html><body><textarea class=""myStudentInput"" id=""myStudentInput1"">Text to be copied</textarea></body></html>";
this.Controls.Add(webBrowser1);
}
private void button1_Click(object sender, EventArgs e) {
var elem = webBrowser1.Document.GetElementById("myStudentInput1");
MessageBox.Show(elem.InnerText);
}
}
Which produces:

Media element not playing audio in windows store app

I want to play mp3 audio in my app but nothing is happening
my code is this
XAML:
<Page
x:Class="SunnahForKids.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:SunnahForKids"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="Black">
<StackPanel Width="900" Height="700" HorizontalAlignment="Center">
<MediaElement HorizontalAlignment="Left" Name="a" AutoPlay="False" Source="azan.mp3" Height="100" Margin="451,299,0,0" VerticalAlignment="Top" Width="100" Volume="100"/>
<Button Content="Button" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,676,0" Height="189" Click="Button_Click"/>
</StackPanel>
</Grid>
</Page>
And here is my .cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Data.Json;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Media.SpeechSynthesis;
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238
namespace SunnahForKids
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
a.Play();
}
}
}
and I am struggling with it for 2 days need help . I also followed this link MediaElement in WinRT / Win8 does not work at all
to update my driver but it is up to date.Display driver in Intel R(Q35) express chipset family ( Microsoft Corporation WDDM-1.0) please get me out of here..
I tried your code and the code works, so something strange is going on. I would try to listen for the events mentioned in the link (example further down for devs looking for code example).
Check your event log on the computer (Windows => Applications), if you've missed updates for windows, and check that that mp3 will otherwise play.
Listen for the MediaFailed event (recommend that you do in the docs) and see if you can grab some information there.Code from msdn:
private void videoMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
// Check the event arguments => e
// get HRESULT from event args
string hr = GetHresultFromErrorMessage(e);
// Handle media failed event appropriately
}
private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e)
{
String hr = String.Empty;
String token = "HRESULT - ";
const int hrLength = 10; // eg "0xFFFFFFFF"
int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal);
if (tokenPos != -1)
{
hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength);
}
return hr;
}

how to make tooltip move along with mouse?

I am using Silverlight 3 + VSTS 2008. I have a image (Multiscale image control) and I place tooltip on this image. The function of Tooltip works fine. Since the image is big (about 500 * 500 size), and since end user could move mouse on the picture, and I want to display tooltip position along with the mouse (i.e. when mouse moves, I want to tooltip move along with the mouse). Currently, the tooltip displays at a fixed position.
Here is my current XAML code, any ideas how to solve this issue?
<MultiScaleImage x:Name="msi" Height="500" Width="500">
<ToolTipService.ToolTip>
<ToolTip Content="Here is a tool tip" Name="DeepZoomToolTip"></ToolTip>
</ToolTipService.ToolTip>
</MultiScaleImage>
I ended up having a similar issue and solved the issue by using a popup. This post contained the core solution. Here is the suggested XAML from the other post:
<Canvas x:Name="LayoutRoot" Background="White">
<Image Source="/pretty-pretty.png" MouseMove="Image_MouseMove" MouseLeave="Image_MouseLeave"/>
<Popup Name="DeepZoomToolTip">
<Border CornerRadius="1" Padding="1" Background="Azure" IsHitTestVisible="False">
<TextBlock Text="Here is a tool tip" />
</Border>
</Popup>
</Canvas>
And here is the suggested, this would go in the code behind:
private void Image_MouseMove(object sender, MouseEventArgs e)
{
DeepZoomToolTip.IsOpen = true;
DeepZoomToolTip.HorizontalOffset = e.GetPosition(LayoutRoot).X;
DeepZoomToolTip.VerticalOffset = e.GetPosition(LayoutRoot).Y;
}
private void Image_MouseLeave(object sender, MouseEventArgs e)
{
DeepZoomToolTip.IsOpen = false;
}
The tooltip control is designed to pop up roughly where the mouse meets the element to which it's bound, and can't respond to move events. Below is a custom tooltip example. I added the background and the z-index so that the TextBlock would appear over the image. The offset from the mouse position keeps the tooltip away from the mouse cursor, so that the movement is animated smoothly.
XAML:
<UserControl x:Class="ImageEditor.TestControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800" Height="800">
<Canvas x:Name="MainCanvas">
<Border x:Name="tt" Background="Gray" Visibility="Collapsed" Canvas.ZIndex="10">
<TextBlock x:Name="txtTooltip" Width="90" Height="20" Text="This is a tooltip" ></TextBlock>
</Border>
<Image x:Name="theImage" Source="images/image.jpg" Width="300" MouseEnter="theImage_MouseEnter"
MouseMove="theImage_MouseMove" MouseLeave="theImage_MouseLeave">
</Image>
</Canvas>
</UserControl>
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace ImageEditor
{
public partial class TestControl : UserControl
{
private bool _tooltipVisible = false;
public TestControl()
{
InitializeComponent();
}
private void theImage_MouseMove(object sender, MouseEventArgs e)
{
if (_tooltipVisible)
{
tt.SetValue(Canvas.TopProperty, e.GetPosition(theImage).Y - (5 + txtTooltip.Height));
tt.SetValue(Canvas.LeftProperty, e.GetPosition(theImage).X - 5);
}
}
private void theImage_MouseEnter(object sender, MouseEventArgs e)
{
_tooltipVisible = true;
tt.Visibility = Visibility.Visible;
}
private void theImage_MouseLeave(object sender, MouseEventArgs e)
{
_tooltipVisible = false;
tt.Visibility = Visibility.Collapsed;
}
}
}

Categories

Resources