Guardian’s Home

.NET, Software Developing, Mammuths and various Life Things…

Archive for the ‘window’ tag

Silverlight: simulate a ‘Windows’ desktop application - part 3 (Resizable Window)

with one comment

It’s time to write the last chapter of this series and take a look at how the resizing capability of the Window is implemented. Since the last time some new feature were added:

Code ported to Silverlight 2.0 RTW.
Solution refactored, the control is now available as a standalone assembly.
The template of the Window has [...]



kick it on DotNetKicks.com

Written by Guardian

November 8th, 2008 at 4:46 am

Posted in Silverlight

Tagged with , , ,

Silverlight: Controls inside ScrollViewer - horizontal resize layout bug and related workaround

with one comment

I was updating my sample on how to simulate a Window environment in Silverlight, after having implemented some fixes to the resize functions and having added the support for scrollbars inside a window, I realized a simple test form with a series of textboxes.
During the horizontal resize operation I encountered a very strange behavior: [...]



kick it on DotNetKicks.com

Written by Guardian

October 16th, 2008 at 9:19 am

Posted in Silverlight

Tagged with , ,

Silverlight: Dropdown Menu Control

with 10 comments

In a previous post I showed how we can build a simple menu control for Silverlight 2 Beta 2 (Silverlight: how to build a simple menu control); then with the release of Silverlight RC0 some things were changed and the menu stopped working, due to how events are handled for disabled controls (Silverlight 2 RC0 [...]



kick it on DotNetKicks.com

Written by Guardian

October 7th, 2008 at 8:24 am

Silverlight: simulate a ‘Windows’ desktop application - part 2 (Dragging Window)

with one comment

void captionBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (DraggingEnabled) { // Bring the panel to the front Canvas.SetZIndex(this, currentZIndex++); // Capture the mouse ((FrameworkElement)sender).CaptureMouse(); // Store the start position this.initialDragPoint = e.GetPosition(this.Parent as UIElement); this.initialWindowLocation.X = Canvas.GetLeft(this); this.initialWindowLocation.Y = Canvas.GetTop(this); // Set dragging to true this.isDragging = true; } }

void captionBar_MouseMove(object sender, MouseEventArgs e) { if (this.isDragging) { Point position = e.GetPosition(this.Parent as UIElement); Canvas c = this.Parent as Canvas; // Move the panel double X = initialWindowLocation.X + position.X - initialDragPoint.X; if ((X >= 0) && (X + captionBar.ActualWidth <= c.ActualWidth)) Canvas.SetLeft(this, X); double Y = initialWindowLocation.Y + position.Y - initialDragPoint.Y; if ((Y >= 0) && (Y + captionBar.ActualHeight <= c.ActualHeight)) Canvas.SetTop(this, Y); } }

note that here we also set some limits for the position that the window can assume, actually we do not want to drag the window outside the Silverlight application area.

void captionBar_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (DraggingEnabled) { // Release the mouse ((FrameworkElement)sender).ReleaseMouseCapture(); // Set dragging to false isDragging = false; } }

 

See you next time.

Example Solution:

SimulatingWindowsV2.zip

 

Technorati Tags: ,,,,

-->

With the release of Silverlight RC0 it was finally time to update this sample application. Let’s now go on with the tutorial and take a look at how the dragging capability of the Window is implemented, you can state if you want to enable or disable the dragging feature for an instance of the control [...]



kick it on DotNetKicks.com

Written by Guardian

September 30th, 2008 at 8:57 am

Posted in Silverlight

Tagged with , ,

Silverlight: how to build a simple Menu Control

with one comment

public void AddSubmenu(SubMenu sm) { if (p == null) { p = new System.Windows.Controls.Primitives.Popup(); pnl = new StackPanel(); p.Child = pnl; MenuLayout.Children.Add(p); } pnl.Children.Add(sm); sm.ParentMenu = this; EnablePopupProvider(); }

 

Menu and SubMenu items have a ParentMenu property to trace who’s his parent cause when you click on a SubMenu you have to close the popup which is holded by it’s parent control; the first attempt to identify a parent menu of a submenu was made by using a while cycle looking at the ‘Parent’ propery of each control that is the visualization tree until we found a ‘Menu’ or ‘SubMenu’ control, so for a Level 1 SubMenu the sequence could have been something like this:

MenuButton –> Grid –> StackPanel –> Popup –> Menu Control

but it came out that the parent property of a StackPanel inside a Popup control returned null..so we were forced to add a ParentMenu property and populate it on the Add() and Remove() functions.

The last thing to note was a ‘Bug’ in the state management of the control, when you use a menu you usually open it up by clicking on the menu voice which will open the popup, then you select a subitem hovering on it with the mouse (the control changes its state to ‘mouseover’) then you click on it (‘pressed’ state); at this point, on the internal button click event 2 things happens: the subitem ask it’s parent to close the menu popup and a MenuClick event is raised for the application to handle it.

But the popup closes itself when the mouse is STILL over the submenu item and the internal control will retain its ‘mouseover’ status (the menu is highlighted) and when you open the popup again you get the last item you clicked on still highlighted.

The solution is to ask the VisualStateManager to change the state of each control to ‘Normal’ each time the popup is shown, so we add the call on the Loaded event (which is called every time we open the popup) of the SubItem control, here’s the code:

void SubMenu_Loaded(object sender, RoutedEventArgs e) { //force the visualization to switch to the normal state to avoid the //hovering bug: the control is in the hover state when we ask to close //the popup that contains it, if we do not change the state manually //it will retain the 'mouse hover' status if (IsEnabled) VisualStateManager.GoToState(MenuButton, "Normal", true); }

 

In the end, let’s have a look at how you can use it:

//Dynamically buildup a menu Menu m1 = new Menu("Test1"); m1.AddSubmenu(new SubMenu("Sub1")); m1.AddSubmenu(new SubMenu("Long Sub1 string")); SubMenu sb1 = new SubMenu("Disabled Sub1"); sb1.IsEnabled = false; m1.AddSubmenu(sb1); sb1 = new SubMenu("Click Me! Sub1"); sb1.MenuClick += new MenuClickEventHandler(sb1_MenuClick); m1.AddSubmenu(sb1); Menu.AddMenu(m1); Menu m2 = new Menu("Disabled Test2"); m2.IsEnabled = false; m2.AddSubmenu(new SubMenu("Sub2")); m2.AddSubmenu(new SubMenu("Sub2")); m2.AddSubmenu(new SubMenu("Sub2")); Menu.AddMenu(m2); Menu m3 = new Menu("Test3"); m3.AddSubmenu(new SubMenu("Sub3")); m3.AddSubmenu(new SubMenu("Sub3")); SubMenu sb3 =new SubMenu("Sub3"); m3.AddSubmenu(sb3); Menu.AddMenu(m3);

This is only the first version of this control, and there’s room for LOTS of improvements (like adding a child menu to each submenu; and a better templating system), but nonetheless it’s a good starting point for your own menu control.

Example Solution:

SilverlightMenu.zip

Technorati Tags: ,,,,

-->

In my current project I needed to use a dynamic Menu Control (a completely declarative approach hardcoding the structure in XAML wasn’t a good fit for my application cause I need to dynamically add, remove, enable and disable menu items). Since Silverlight doesn’t support a menu control yet I had 3 options:
1- look for a [...]



kick it on DotNetKicks.com

Written by Guardian

September 25th, 2008 at 6:33 am

Posted in Silverlight

Tagged with , , , ,