RoutedEventArgs error - silverlight

Basically a RoutedEvent travels through the Logical tree, either from top to bottom (Bubble event route) or bottom to top (Tunnel event route).
What this means is that if you have a Button inside of a StackPanel, that itself is inside of a Grid;
if you define a Click event in the controls they will all trigger it unless one of them handles it.
In my application I have:
Button -> StackPanel -> Grid
If it’s true, that StackPanel and Grid will not trigger it.
Grid -> StackPanel -> Button
if the Grid handles it, the StackPanel and Button will not trigger it.
in my application i wrote:
private void Button_Click(object sender, RoutedEventArgs e) {
Response.Redirect("http://www.legalbill.com");
}
it gave this-
Error - The name 'Response' does not exist in the current context …
what it means n how will i move to desired site?

Are you sure you are trying to do this in wpf ? If you are trying in silverlight,It doesn't support Response.Redirect.You can use like this
using System;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
namespace Tips
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// execute one of them.
HtmlPage.Window.Navigate(new Uri(“http://www.silverlight.net“));
HtmlPage.Window.Navigate(new Uri(“http://www.silverlight.net“), “_blank”);
HtmlPage.Window.Navigate(new Uri(“http://www.silverlight.net“), “_blank”, “toolbar=0″);
}
}
}

Related

(WPF) Does .Close() method releases the window instance?

I'm creating a new window in On_Click method. First I tried this;
public partial class MainWindow : Window
{
CustomerOperations customerOperationsWindow;
public MainWindow()
{
customerOperationsWindow = new CustomerOperations();
InitializeComponent();
}
private void btnCustomer_Click(object sender, RoutedEventArgs e)
{
customerOperationsWindow.Owner = this;
customerOperationsWindow.Show();
}
}
It's not working so I started creating the window instance every time the user clicks on the Customers button. And I used the following codes.
private void btnCustomer_Click(object sender, RoutedEventArgs e)
{
CustomerOperations customerOperationsWindow = new CustomerOperations();
customerOperationsWindow.Owner = this;
customerOperationsWindow.Show();
}
In the new window, If user clicks to Main button, I want to navigate to main window.
private void btnMain_Click(object sender, RoutedEventArgs e)
{
this.Close();
this.Owner.Show();
}
First question: Does this.Close() releases the window instance?
Second question: Is this usage correct?
What do you think is the best practice?
Thank you all.
Window.Close() will dispose all resources allocated by the instance. That's why you cannot show it again once it was closed.
If you want to reuse the same Window instance, you should cancel the closing procedure to prevent disposal of internal resources and collapse the Window instead (by setting Window.Visibility to Visibility.Collapsed - Visibility.Collapsed is also the default value of an instantiated Window before Window.Show() is called).
Alternatively hide the Window by calling Window.Hide() (which will set the Visibility to Visibility.Hidden) instead of Window.Close().
Calling Window.Show will also set the window's visibility to Visibility.Visible.
As a matter of fact, showing a Window by setting Window.Visibility is the asynchronous version of Window.Show().
Generally, you switch between Window instances by using the Window.Activate method. Calling Window.Show on a Window that is currently showing/visible, does nothing.
public partial class MainWindow : Window
{
CustomerOperations CustomerOperationsWindow { get; }
public MainWindow()
{
InitializeComponent();
this.CustomerOperationsWindow = new CustomerOperations();
// Consider to move this logic to CustomerOperations class,
// where you can override the OnClosing method instead of subscribing to the event
this.CustomerOperationsWindow.Closing += CollapseWindow_OnClosing;
}
// Cancel close to prevent disposal and collapse Window instead
private void CollapseWindow_OnClosing(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.CustomerOperationsWindow.Visibility = Visibility.Collapsed;
this.CustomerOperationsWindow.Owner.Activate();
}
private void btnCustomer_Click(object sender, RoutedEventArgs e)
{
this.CustomerOperationsWindow.Owner = this;
// Calling Show will set the Visibility to Visibility.Visible
this.CustomerOperationsWindow.Show();
}
}
Creating a Window instance allocates unmanaged resources. If this happens very frequently, you will keep the garbage collector busy. From a performance point of view you may want to avoid it and prefer to reuse the same instance.
In a common scenario this is not necessary. But since Window exposes a Hide() method, you may consider to use it instead of Close().
If you want to switch to the parent window, you can use the code this.Owner.Activate(); and if you want to close the current window, first this.Owner.Activate(); and then this.Close();.
When you enter this.Close(), the compiler does not execute the following lines after reaching it. And when a sample window still exists there is no need to recreate it
private void btnMain_Click(object sender, RoutedEventArgs e)
{
this.Owner.Activate();
this.Close();
}

WPF: Accessing children elements from a base class

I'm trying to create add some custom behaviors to a ScrollViewer in WPF / XAML. Specifically, the ScrollViewer should scroll to the top any time the Thumb in the VerticalScrollBar is clicked. Likewise, it should scroll to the left any time the Thumb in the HorizontalScrollBar is clicked.
I would think the easiest way of doing this would be to create a new class that inherits from ScrollViewer, and then subscribes to the MouseDoubleClick event on the VerticalScrollBar's Thumb and on the HorizontalScrollBar's Thumb.
For example:
public class DoubleClickScrollViwer : ScrollViewer
{
public DoubleClickScrollViwer()
: base()
{
//Find the Horizontal and Vertical scroll bars. Subscribe to the thumb's double click.
}
void VerticalScrollBarThumb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
this.ScrollToTop();
}
void HorizontalScrollBarThumb_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
this.ScrollToLeft();
}
}
I can't seem to find a way to drill down to the children from the DoubleClickScrollViewer. How can I drill down to the children? Or is there a better way of going about this?

How to pass main page method to Childwindow Silverlight?

This has been my issue for long time,i have a method inside the main page to open application,
i need to invoke it when the user control is clicked. so when creating a usercontrol i need to pass this method and call it inside the usercontrol click event.
Right now am doing like this,
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MainPage m = new MainPage();
m.openApplication("STOCK");
}
But its throwing a null reference exception.Help me on this.
Rather than creating an instance of your mainpage, you can pass the mainpage as an argument to the usercontrol and do it like below,
Usercontrol(Mainpage m);
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
m.openApplication("STOCK");
}
Creating an instance will make null references if values are not assigned.

Second Window Positioning WPF

I have MainWindow which has a button that allows it to open another WPF Window. I want this window to open always on the right hand side of the MainWindow practically right next to it.
How can I do this? This needs to work even if the width of the MainWindow changes as I have various buttons on the MainWindow that can change the size of the MainWindow depending on what panel is visible.
You can calculate where you want the new window if you have a reference to the other window.
Get the other windows position by accessing the Left and Top properties and its width by accessing the ActualWidth or the Width property.
Now you can calculate the new windows position by adding Left + Width + Some spacing.
Check out the documentation for the Left property here:
http://msdn.microsoft.com/en-us/library/system.windows.window.left.aspx
The others behave similarily.
You need to set manual startup location for second window in properties or in code:
WindowStartupLocation = WindowStartupLocation.Manual;
On events Loaded, SizeChanged, LocationChanged of first window, you should adjust position of second window like this:
public void AdjustPosition()
{
window2.Left = Application.Current.MainWindow.Left + Application.Current.MainWindow.ActualWidth;
window2.Top = Application.Current.MainWindow.Top;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
AdjustPosition();
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
AdjustPosition();
}
void MainWindow_LocationChanged(object sender, EventArgs e)
{
AdjustPosition();
}

Problems using inheritance with drag and drop in WPF

I have a usercontrol that I want to implement a drag and drop interface on, here are the vital parts of the implementation, and this works fine:
XML-file to the usercontrol to be draggable:
<UserControl
...default xmlns...
MouseLeftButtonDown="Control_MouseLeftButtonDown">
...GUI-ELEMENTS in the control...
</UserControl>
Code behind:
public partial class DragableControl : UserControl
{
private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragDrop.DoDragDrop(this, this, DragDropEffects.Move);
}
}
XML-file to the usercontrol which will be able to accept a drag and drop operation:
<Usercontrol
...default xmlns...>
<Grid AllowDrop="True" Drop="Grid_Drop">
... GUI elements in the grid....
</Grid>
</Usercontrol>
Code behind:
public partial class DropClass: UserControl
{
private void Grid_Drop(object sender, DragEventArgs e)
{
var control = (DragableControl)e.Data.GetData(typeof(DragableControl));
if(control != null)
{
//do something
}
}
}
To be able to create different usercontrols which have drag and drop functionality, I creates a base class, BaseDragableUserControl, which at the moment contains nothing, but inherits from usercontrol.
Code:
public class BaseDragableUserControl: UserControl
{
}
I change my code (both xaml and code):
public partial class DragableControl : UserControl
I also changes the class for receiving to this:
public partial class DropClass: UserControl
{
private void Grid_Drop(object sender, DragEventArgs e)
{
var control =(BaseDragableUserControl)e.Data.GetData(typeof(BaseDragableUserControl));
if(control != null)
{
//do something
}
}
}
But the control variable is always Null. I guess that the getdata in the DragEventsArgs does not like inheritance. Is there a way to achieve this? To make it possible to use a base class for a drag and drop class?
Instead of passing this when you initiate the drag/drop, create one of the standard containers for that purpose. Specifically:
DragDrop.DoDragDrop(this, new DataObject("myFormat", this), DragDropEffects.Move);
and then now you know to expect a specific kind of data:
var control =(BaseDragableUserControl)e.Data.GetData("myFormat");

Resources