Posts

Selecting WPF ListView row in code

An app I am considering migrating to WPF uses a DevExpress grid to show a list of events, as I animate my composition I highlight the current event in the grid. Things I have learned about WPF: 01: You can create a data grid type view like so: <ListView Name="ListView">   <ListView.View>     <GridView AllowsColumnReorder="False">       <GridViewColumn DisplayMemberBinding="{Binding Path=Title}" Header="Title"/>       <GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="FirstName"/>       <GridViewColumn DisplayMemberBinding="{Binding Path=LastName}" Header="LastName"/>     </GridView>   </ListView.View> </ListView> 02: Don’t put a ListView in a <StackPanel>! It renders all of the data at once. When you have 6K rows of data for example it takes about 1 second to select the next row. Instead you should put it in a grid <Gri

Creating a drop shadow

Requirement: Take a PNG image that has an alpha mask and from it generate a new bitmap which has the same alpha mask but every pixel is black. Solution: private Bitmap GenerateDropShadowImage(Image image) {   Bitmap bitmap = new Bitmap(image);   BitmapData bits = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);   IntPtr bottomScanLine = bits.Scan0;   int bytesPerRow = bits.Width * 4;   unsafe   {     byte* pixelValue = (byte*)bottomScanLine.ToPointer();     for (int count = 0; count < bits.Width * bits.Height; count++)     {       pixelValue[0] = 0;       pixelValue[1] = 0;       pixelValue[2] = 0;       pixelValue = pixelValue + 4;     }   }   bitmap.UnlockBits(bits);   return bitmap; }

WPF custom button templates

Image
I’ve had a bit of a play with WPF for the first time today. I decided to create my own button template. The idea of a template is that you can redefine the visual elements that make up the control. Create a new WPF application Now add a button within the grid like so  <Grid>   <Button Content="Click me" Width="150" Height="50"/>  </Grid> So that we can see what we are designing add a gradient background to the window. Within the <Window> node add  <Window.Background>   <LinearGradientBrush>    <GradientStop Color="Black" Offset="0"/>    <GradientStop Color="White" Offset="1"/>   </LinearGradientBrush>  </Window.Background> Now to start designing the button template. Within the <Window> node add  <Window.Resources>  </Window.Resources> this is where we will add the template, within that new node add the following  <ControlTemplate x

Binary response in ASP MVC

Today I wanted to give access to certain files on a website only via my DownloadController. This was so that I could ensure the current user had purchased the item in question first, and also sign any license info into the download aswell. I tried getting a URL like this to work http://localhost/download/1/SomeFileName which would remap to the DownloadController public void Index(int id, string fileName) This worked fine, and because the URL ended with "SomeFileName" it would get saved as the correct filename too, but this was no use because SomeFileName has no file extension. As soon as I added .zip on the end the request no longer went via the new HttpHandler in the MVC web extensions. Even when I added it in the <httpHandlers> section of web.config it just wouldn’t work. My problem was in relying on the url for the filename. This is apprarently not the way it should be done. Instead I should have stuck to the standard URL approach http://localhost/download/1 an

Silverlight and webservices

First download the binaries you need from here: http://silverlight.net/GetStarted/ Next run VS2008 and create a new project. Select the Silverlight node and then the Silverlight Application node. ProjectName = MySilverlightApp Tick the checkbox "Create directory for solution" Click OK On the wizard page you want the default values: * Add a new page to the solution for hosting the control Project type = Web Site Name = MyWebService Now delete the two ASPX files, we wont be needing those. Rename the HTML page to Index.html and set it as the project start page. Right click the website project and select "Add new item". Select "Web Service". Name = DateTimeService.asmx Click ADD Change the HelloWorld method to public DateTime GetServerDateTime() {   return DateTime.Now; } Right-click the References node on the Silverlight project and select "Add service reference". Click the "Discover" button, and in the tree view that appears select the Da

Postal codes within a radius

My hobby MVC website allows people to place adverts. When searching for adverts I would like the user to be able to specify a UK postal code and radius to filter the adverts down to ones within travelling distance. The trick to this was to record a list of UK postal codes and their latitude/longitude. The first step is to write a routine which will give a straight line distance between to coordinates: public static class MathExtender {   public static double GetDistanceBetweenPoints(double sourceLatitude, double sourceLongitude, double destLatitude, double destLongitude)   {     double theta = sourceLongitude - destLongitude;     double distance =       Math.Sin(DegToRad(sourceLatitude))       * Math.Sin(DegToRad(destLatitude))       + Math.Cos(DegToRad(sourceLatitude))       * Math.Cos(DegToRad(destLatitude))       * Math.Cos(DegToRad(theta));     distance = Math.Acos(distance);     distance = RadToDeg(distance);     distance = distance * 60 * 1.1515;     return (distance);   }   p

Test Driven MVC and ECO

I have decided that mocking IEcoServiceProvider is not the way to go. Your controller will use the mocked provider during testing but   You don’t want to have to mock every service the provider may return, it’s a lot of work!   You don’t want your controller using a mocked service, and then the EcoSpace using the real one! At first I was mocking every possible service request. IUndoService, IPersistenceService, IOclService, etc. I get bored typing them out in this blog, so doing it in tests was really annoying me. I decided I would instead only mock the service in question. So if I were ensuring that an action wont save an object with broken constraints I would mock GetEcoService<IConstraintProvider> and ensure that I always got a broken constraint. The problem was that the test to ensure I can save a valid object would then invoke the IPersistenceService.UpdateDatabaseWithList method. In my EcoSpace I have decorated my persistence service so that it checks every object i