How To Navigate From Child Window To User Control Silverlight? - silverlight

I have a child window with two buttons. When a button click event occurs I need to navigate to a specific user control. How to do this in Prism Framework?

If you are using PRISM will be simple as fires a notification event using the event aggregator. Anyway if not, this will solve your problem.
You need when you fire the childwindow to add the close event to that childwindow, like this:
YourChildWindow childWindow = new YourChildWindow ();
childWindow.Closed += new EventHandler
(YourChildWindowClosed);
childWindow.Show();
Then,
?
void YourChildWindowClosed(object sender, EventArgs e)
{
YourChildWindow yourChildWindow = ((YourChildWindow)sender);
if
(yourChildWindow.DialogResult.Value)
{
... do your things here ...
}
}
I hope this can help you.

Related

What event is raised on Grid.Children.Add

In my WPF application, I have a single Main window with a Grid. The Login and Shell are 2 separate UserControls added as children to a grid. I need to find out when the Shell is loaded and start a timer from the Main window.
I just need to know as to what event is raised when a UserControl is added using Grid.Children.Add method, so that I can check if Login is loaded or the Shell and start the timer.
I'm not quite sure what you're trying,
but it sounds like you're looking for the Load event:
UserControl MyControl = new UserControl();
MyControl.Loaded += new RoutedEventHandler(MyControl_Loaded);
public void MyControl_Loaded(object sender, RoutedEventArgs e)
{
if (((UserControl)sender).IsLoaded)
{
..... do something
}
}
Hope it helps

Hotkeys in Silverlight?

Is there a way to create global keys in Silverlight? I added a KeyDown event handler to the topmost Page element. I can catch the key signal when elements like a Button, TextBox, Calendar, and RichTextBox have focus. But the DatePicker won't let me handle the key down event at the Page level. Any way to make a hotkey in non-OOB SL?
P.S. I am assuming that the DatePicker control behaves differently than others as it is still in the Silverlight Toolkit.
have you tried using the AddHandler method? With this method you can add a handler and define if you want to handle already handled events, too.
In my simple example I added it to the RootVisual in the app.xaml.cs.
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
RootVisual.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(HandleKeyDown), true);
}
private void HandleKeyDown(object sender, KeyEventArgs e)
{
//Add your keyhandling code here
Debug.WriteLine(e.Key);
}
I tried it with a DatePicker and it works.
Hope this helps!
BR,
TJ
This may help you. The author provides two ways using HTML bridge (when your Silverlight does not have focus) and using the KeyUp event on Application.RootVisual.
I have not tried it myself so I wouldn't know if it's working.

Click event in UserControl- WPF

I have a UserControl in my WPF application.
I want to call a click event and do some things when the user clicked the UserControl.
The problem is- the UserControl doesn't have a click event.
I searched on the web and found that you can use the MouseLeftButtonUp event.
I tried it- but it doesn't respond to my clicks.
You didn't write what you are trying to do but if you need a click event maybe you are writing some kind of button (the Button class is actually "something you can click" with the visual representation in a control template you can replace)
If you need a button with complex content inside - put your user control inside a button
If you need a button that doesn't look like a button write a custom control template for button
If you need a button with extra functionality subclass button, add the extra data/behavior in code and put the display XAML in a style.
I think for your needs PreviewMouseLeftButtonUp(Down) event is more suitable. Then you need to handle ClickCount for counting amount of clicks and then raise your own event, on which other controls will know, that your control is clicked. There are much more methods on handling click event. You should look at this msdn article and this
UPDATE to handle both Click and DoubleClick
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
_myCustomUserControl.MouseLeftButtonUp += new MouseButtonEventHandler(_myCustomUserControl_MouseLeftButtonUp);
_myCustomUserControl.MouseDoubleClick += new MouseButtonEventHandler(_myCustomUserControl_MouseDoubleClick);
}
bool _doubleClicked;
void _myCustomUserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
_textBlock.Text = "Mouse left button clicked twice";
_doubleClicked = true;
e.Handled = true;
}
void _myCustomUserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_doubleClicked)
{
_doubleClicked = false;
return;
}
_textBlock.Text = "Mouse left button clicked once";
e.Handled = true;
}
}
To test this example name your control as _myCustomUserControl and add a TextBlock named _textBlock to your MainWindow.xaml
Why not just use MouseDown?
Put the event in the User Control and simply do this:
private void MyControl_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
MessageBox.Show("Clicked!");
}
}

Detect when application becomes active

In MDI application which event can the child Form class use to detect when the application becomes active?
I tried Form.Acivated event but it occurs only when the form is activated and doesn't when the application gets focus.
It is the MDI parent form that gets the Activated event. You can subscribe to the event in your child form's Load event. Be careful, you have to make sure you unsubscribe the event when the child gets closed or you'll leak the child form instance. Make it look like this:
protected override void OnLoad(EventArgs e) {
var main = this.MdiParent;
main.Activated += main_AppActivated;
this.FormClosed += (o, ea) => main.Activated -= main_AppActivated;
}
void main_AppActivated(object sender, EventArgs e) {
// Etc...
}
Have you tried the GotFocus event?
While WPF has such a notion, WinForms does not to the best of my knowledge; you'd need to use Form-level events (like GotFocus from the earlier answer).

winform mdi catch child form focus c#

I would like to be able to catch a child window focus event, in an mdi form.
If i loose focus to the entire mdi application, then click on a child, it works, but if I had two child forms open, I cannot catch the focus event when clicking between them.
I am using Dotnet Framework 2.0, and I need the code solution that will run fine on a windows 2000 machine, and up.
Thanks in advance for all help and advice,
-regards
Jeremy
I think you're looking for the Form.MdiChildActivate event. This event will be fired in your MDI parent form.
override the child forms Activated event.
sample code:
private void addChild(){
frmChild mychild = new frmChild();
mychild.Activated += FActivated;
mychild.MdiParent = this;
mychild.Show();
}
private void FActivated(object sender, EventArgs e)
{
MessageBox.Show("Activated one of the child.");
}

Resources