Skip Ribbon Commands
Skip to main content

Blog

:

Quick Launch

Home
Blog of Jeff Veatch, containing my thoughts, comments and questions.
August 11
Finding a control’s position in Silverlight 2.0

Ever found yourself with a need to know a controls position in silverlight 2.0, but weren’t using a canvas layout?

You could be implementing a combo box, or a review popup, or a semi-transparent detail panel on a glass pane, and suddenly found that the control you were hoping to trigger this behavior from has no idea where it is.  This triggering control, of course, it positioned using a Grid or StackPanel, so it will layout nicely in a variety screen sizes and shapes.  Unfortunately, this means it doesn’t have a X or Y position. Does this mean you have to convert your app to use just a Canvas and implement your own layout logic?

The answer is no, you can find the position using UIElement.TransformToVisual() on the Current Application’s RootVisual.  The parameter you pass is the control whose location is of interest.  This method returns a transform object.

GeneralTransform myButtonTransform = Application.Current.RootVisual.TransformToVisual(myButton);

Now for this to work, the actual object returned by TransformToVisual needs to be of the type MatrixTransform . Typically the RootVisual is a UserControl, and providing another transform hasn’t been applied, it meets this requirement ( as of SL 2.0 Beta2). 

if (myButtonTransform is MatrixTransform)
{
      double xPosition = ((MatrixTransform)myButtonTransform).Matrix.OffsetX;
      double yPosition = ((MatrixTransform)myButtonTransform).Matrix.OffsetY;
      // do stuff with the positions...
}

 

Oops! 

The values of xPosition and yPosition have been loaded with are negative values, so we’ll need to make them positive before using them.  What follows are the xPosition and yPosition being converted to positive values, each a different way.

 

      double xPosition = Math.Abs(((MatrixTransform)myButtonTransform).Matrix.OffsetX);

      double yPosition = -(((MatrixTransform)myButtonTransform).Matrix.OffsetY);

 

Of course, multiplying by -1 would also work.

 

 

March 04
How to filter Collections on the fly, using LINQ

Back in February a fellow Vertigo employee, Alan, blogged about “WPF: How to filter ListBox items on the fly”, which got me thinking about how could I do the same thing with LINQ.

Additionally, as I rarely have the luxury to be working with ObjectDataProviders, SqlDataProviders, DataSets, and such, a solution that worked with collections of Business Objects, which typically don’t have a “Filter” method, would be very useful.

LINQ + Collections.Generic

So with LINQ working with the generic collections, implementing a filter becomes very easy. 

In the below code I filter a List<> of NameEntry objects for ListBox(lstNames) based on the value entered into a TextBox(txtFilter).  NameEntry has several properties, but we’re only interested in Name for now.

List<NameEntry> nameList = NameEntry.GetTestDataList();

 

      var filteredList = from item in nameList

          where item.Name.ToLower().Contains(txtFilter.Text.ToLowerInvariant())

          select item;

 

      lstNames.DataSource = filteredList;

      lstNames.DataValueField = "Name";

      lstNames.DataBind();

The highlighted code is all you need to accomplish this simple filter.

Alternatively, you could replace the entire LINQ statement with a call to an extension method that LINQ adds to Generic Collections called Where<>(), like so:

var filteredList = nameList.Where(item =>
      item.Name.ToLowerInvariant().Contains(txtFilter.Text));

Which has even fewer characters of code; although for those unaccustomed to lambdas it can be harder to read.

3/24/08 - Corrected sample to use ToLowerInvariant()
October 10
Firefox + Noscript = no silverlight

One of the things I like about the Firefox browser is the add-on “Noscript” which allows you to selectively choose which sites to allow javascript on. 

But I ran into a problem viewing pages with Microsoft’s new Silverlight content. 
As in it doesn’t work… at least with NoScript with its default settings.

But don’t worry, there is a solution, and it’s an easy one.  One of the options for the NoScript, is “Forbid Microsoft  Silverlight”, which by default is forbidding silverlight.

There are two ways to access NoScript options.

1>     Use the menu: Tools>Add ons
find the NoScript entry and click Options

Add-on options screen shot

2>     Use the context menu on NoScript’s icon on the status bar

Contest menu screen shot

Once in the NoScript Options panel, go to the Plugins tab

Just uncheck “Forbid Microsoft Silverlight” and you are good to go!

Options dialog screen shot

Now if someone could tell me how to keep this dialog from coming up every 45 seconds when viewing non-interactive Silverlight animations:

Unresponsive screen shot

May 09
Running another app from a Vista Gadget

When executing an application from inside a Vista Gadget you have at least two ways to go. 

One, is to use the System.Shell.execute.

The other is two get an instance of a WScript.Shell, and call it’s run method.

Using System.Shell.execute seems the cleaner of the two approaches, and seems to the be advised way to go, but it is convenient to use WScript’s run when you need to process a command line with environment variables.

In this example both calls will try to start the same cassini server:

 

                var executable = "C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\webdev.webserver.exe";

                var executableWithEnvironment = "%WINDIR%\\Microsoft.NET\\Framework\\v2.0.50727\\webdev.webserver.exe";

                var params =      "/port:80 /path:\"/*path to webpage*/”

                System.Shell.execute(executable,          params);             

                var shell = new ActiveXObject('WScript.shell');

                shell.run(executableWithEnvironment +" "+params);

May 09
‘alert’ calls in a Vista Gadget’s Javascript

One of the first things I noticed while writing my first Vista Gadget, was the lack of support of “alert” in the javascript.  After digging around online I found this easy substitute, using a WScript.Shell object in the javascript:

var shell = new ActiveXObject("WScript.Shell");

shell.Popup(“Hello World”);

May 01
Running the Visual Studio's webserver without Visual Studio

Sometimes you just want to run a ASP.NET webpage locally, but really don't need Visual Studio also running.  Like for a functional review or just to run a finished web app.  On those occasions just run the "WebDev.Webserver" on its own.

 

Where is WebDev.Webserver.exe?

In the  .Net framework 2.0 directory.  Usually located here:

 %WINDIR%\\Microsoft.NET\\Framework\\v2.#.#####

Note: the “#”s represent the version number, and should not be used in the path literally.

 

How to use it.

WebDev.Webserver.exe takes three parameters: the port, the path to the webpage source and optionally the virtual path.

 

WebDev.WebServer /port:<port number> /path:<physical path> [/vpath:<virtual path>]

 

This can be used from command prompt, but a convenient alternative is to simply create a short cut. 

 

When running from a command prompt prefixing your command line with "start /b" will allow the command prompt to return while the sever is running.

 

 
 
April 12
Vista: Always running an app as Admin
For applications that do not fully function under Vista without running as Admin, here's a short-cut that lets you do so by just running the app.

Setting an application to always run in Admin Mode

  • Bring up the property box for the application by selecting the app, clicking the right mouse button and selecting “Properties”:

StartBar

  • Then go to the “Compatibility” tab and check the box for “run this program  as administrator”(highlighted), which is at the bottom in the “Privilege” section.

File Properties

Use of the run as admin should probably be used sparingly, for apps like "SQL Server Management Studio" and "MS Expression Blend", which otherwise lack key functionality.

 

 

April 02
Viewing videos at 1.5 speed with WMP 11 and Vista
When watching instructional videos on Windows Media Player, it can be useful to play them at the "faster" setting, where you can still understand what's being said, but can absorb more content in less time. 
 
To quickly switch to the faster play mode with Windows Media Player 11 on Vista, you can either:
  • right click on the play/pause button and select "Fast Playback"
  • or press "Ctrl+Shift+G". 
 
 
To resume normal play, either use the same context menu and select "Normal Playback" , or press "Ctrl+Shift+N".