Does XAML provide a way to detect if the user's mouse cursor has left the Silverlight window? If so, how would I go about doing this?
Thanks for your help.
Yes there is.
Assuming the the MainPage is your RootVisual and you've added a reference for System.Windows.Browser assembly then the following code should work.
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
//objSilverlight is the <object> tag id
var element = HtmlPage.Document.GetElementById("objSilverlight");
element.AttachEvent("onmouseout", new EventHandler(HandleMouseOut));
}
public void HandleMouseOut(object sender, EventArgs args)
{
//handle your event here
}
}
Basically the .Net event handler is being attached to the onmouseout DOM event in the Html object element that contains the silverlight plugin.
Related
In using MVVM pattern, I have a custom inkcanvas with:
protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e)
{
CustomStroke newStroke = new CustomStroke(e.Stroke.StylusPoints, e.Stroke.DrawingAttributes);
this.Strokes.Remove(e.Stroke);
this.Strokes.Add(newStroke);
InkCanvasStrokeCollectedEventArgs eNew = new InkCanvasStrokeCollectedEventArgs(newStroke);
// Raises the System.Windows.Controls.InkCanvas.StrokeCollected event.
base.OnStrokeCollected(eNew);
}
How do I get the view model to receive the InkCanvas.StrokeCollected event?
I can not bind the XAML to the strokes as the StrokeCollection.CollectionChanged event will be called three times by the custom inkcanvas.
Any help is appreciated.
Try this
public Window3()
{
InitializeComponent();
var vm=new ViewModel();
this.DataContext = vm;
canvas.StrokeCollected += vm.OnStrokeCollected;
}
ViewModel
public class ViewModel
{
public void OnStrokeCollected(object sender, InkCanvasStrokeCollectedEventArgs e)
{
}
}
Edit
if you want to do it without codebehind see the article EventTrigger
You simply bind it via XAML as you already did, which is the correct way to do it.
That you get 3 events, doesn't matter. Just handle the one you need.
For example, if you are only interested in the StrokeCollectedEvent, then just do
protected override void OnStrokeCollected(InkCanvasStrokeCollectedEventArgs e)
{
if(e.RoutedEvent != InkCanvas.StrokeCollectedEvent)
return;
// handle the event
}
For a full list of Events, consult the "Fields" Section of InkCanvas MSDN documentation. The fields ending with "Event" are RoutedEvent constants, which are passed in the InkCanvasStrokeCollectedEventArgs.
I am creating a UserControl, I want that when I click in a button from that Control a property (attriibute) modifies from my MainWindow. The UserControl is created from a separate project and built as a .dll.
I had tried the following:
Window l = Window.GetWindow(this);
The problem is that because my window is not being referenced I have no way to access it (the properties I had created) and I dont know how to do it. If I try to write "MainWindow" it says that it couldn't be found.
You can get window using Application.Current.MainWindow. It will return window object so make sure you typecast it to actual instance of your window.
Assuming actual instance is MainWindow, it can be accessed like this:
MainWindow window = (MainWindow)Application.Current.MainWindow;
You have a number of ways of accessing a reference to the main Window in WPF. There is the way that #Rohit Vats showed you:
MainWindow window = (MainWindow)Application.Current.MainWindow;
However, as you have noticed, this does not always work. Sometimes it can be fixed simply by setting the property to the MainWindow instance:
public MainWindow()
{
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Application.Current.MainWindow = this;
}
You should now be able to access the MainWindow from this property. However, if that still doesn't work for some reason, then you can also try the Application.Windows property:
foreach (MainWindow window in Application.Windows.OfType<MainWindow>())
{
// Do something with window here
}
So I have a user control within a window. I need to be able (from user control) to retrieve the parent window left and top (in order to locate a new popup I'm opening from the child). I'm trying to do this by referencing the UserControl .Parent property but doesn't seem to work.
Any idea? Thanks!
Are you using MVVM? Are you concerned about writing code in the code behind? .Net 3.5 or 4.0?
From the UserControl Code behind you could use:
Window parentWindow = Window.GetWindow(userControlReference);
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
Loaded += new RoutedEventHandler(UserControl1_Loaded);
//Window parrentWindow = Window.GetWindow(this);//don't add here the value will be null
}
void UserControl1_Loaded(object sender, RoutedEventArgs e)
{
Window parrentWindow = Window.GetWindow(this);
}
}
I have WPF control hosted inside a WinForms control using ElementHost. The WinForms control has a context menu. I want to show the context menu when user right click on the WPF control. How can this be done? It seems mouse event is not bubbled from WPF to WinForms.
it is not automatically bubbled up, as you might have handled it in the WPF control in the first place. However, you can easily add this yourself.
In your WPF user control, expose an event that you trigger on right mouse up:
public event Action ShowContext;
private void rectangle1_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
if (ShowContext != null)
{
ShowContext();
}
}
Then in your winforms control with element host you can use it like so:
public UserControl1 WpfControl { get; set; }
public Form1()
{
InitializeComponent();
WpfControl = new UserControl1();
WpfControl.ShowContext += () => contextMenuStrip1.Show(Cursor.Position);
elementHost1.Child = WpfControl;
....
I have a button inside my UserControl. I have three instances of this UserControl on the same page.
How can I expose the click event of the button inside such that I can assign different events for each instance of my UserControl.
I think this is similar to concept behind exposing DependencyProperty but I don't understand how to do it for events.
Thanks.
I normally add an event of the same name (and same parameters) to the user control and subscribe to the child control's original event, so I can pass the event on:
public partial class ClickEventControl : UserControl
{
public event EventHandler<RoutedEventArgs> Click;
public ClickEventControl()
{
InitializeComponent();
}
private void aButton_Click(object sender, RoutedEventArgs e)
{
if (Click != null)
{
Click(sender, e);
}
}
}
I would also be interested if there is a more general way of doing it.