Archive for the ‘XAML’ category

Draggable WPF Controls

March 19th, 2012

I have a project where I needed to have WPF controls placed on a canvas and those controls have the ability to be dragged or positioned around on the screen somewhere.  I was using some custom logic I had written to draw rectangles and textboxes to the canvas and just watching the mouse events but that was a hack because sometimes the text would get too big to fit in the textboxes.  I came across this blog post and Josh Smith has a nice little DragCanvas class that wraps up all this functionality very nicely, all you have to do is put the controls in the Xaml and you are set, very nice job Josh!

This is nice for me because I wanted to display data in a grid and have headers and be able to drag those grids around the screen.  It also allows you to do other things like set transparencies on those controls and hide and show them as well.

Windows 8 Apps, should I write in XAML or HTML5??

March 12th, 2012

I have been recently doing some research into Javascript and, coming from a .net background, I must admit is a bit daunting due to just my lack of knowledge really.  I have been pondering what road I should stay focused on continuing forward to work on apps to be targeted for future versions of Windows, in particular, Windows 8.

I guess this article should be called, “Is it worth it to learn HTML5 or just stick to what I know in the .Net world?”  I think the answer is pretty simple, I should absolutely continue on and learn all I can about writing apps in HTML5.  I honestly think in the future that “Metro” apps that are written in XAML will only have a very limited market, obviously, cause they are targeted for Windows machines only.  On the other hand, I could write a HTML5 app and it will work virtually work anywhere, that makes too much sense if you ask me.  I also have some years invested in writing WPF/XAML apps so if I am tasked in writing an app in C#/WPF, it won’t be a big deal, I already have those skills, sure I may have to learn a few new libraries, but no big deal.

Javascript has came a long way since I last looked at it several years ago, there are full blown libraries like Backbone.js, that allow you to do MVC programming.  There are literally thousands of libraries to do just about anything you want, with full source to change it however you want.  I don’t want to come across at a Microsoft hater in this post, just posing a question that seems to make too much sense to me.

I am not talking about writing a Javascript app using the WinJS library, which, once again, would tie you to a windows app, I am talking about a full-blown HTML5 app written outside of Visual Studio, so you are sure to not be depending on any kind of Microsoft library.  This whole paradigm is nothing new, I think I am just now getting around to embracing it because its pretty easy to see where things are going, and I think Microsoft is getting behind in this area.  Even still, it never hurts to learn a whole new programming language, and while Javascript is not new to me, I have never taken the time to really learn it and now that there are so many libraries available and it has gotten very powerful, I think its time I gave it a harder look, what are your thoughts?

Save WPF/Xaml controls to Bitmaps

February 17th, 2012

I don’t know if you have ever had the need to save an image of a Xaml/Wpf control but I did recently in a project. The method I have below will do just this:

public List<Bitmap> ExportToPng(List<object> objects) {
            var images = new List<Bitmap>();
            // iterate wpf objects, saving current canvas transform
            foreach (object o in objects) {
                Transform transform = c.LayoutTransform;

                // reset current transform (in case it is scaled or rotated)
                c.LayoutTransform = null;

                // Create a render bitmap and push the surface to it
                var renderBitmap =
                new RenderTargetBitmap(

                System.Drawing.Image ImageWork;
                // Create a memory stream for using image
                using (var stm = new MemoryStream()) {
                    // Use png encoder for our data
                    var encoder = new PngBitmapEncoder();

                    // push the rendered bitmap to it

                    // save the data to the stream
                    ImageWork = System.Drawing.Image.FromStream(stm);
                    images.Add(ImageWork as Bitmap);
                // Restore previously saved layout
                c.LayoutTransform = transform;
            return images;

The method does take an array of objects, of course you could modify it to only do one at a time but I had several I needed in my example. The key thing here is that in your xaml code, make sure you have your control in just a single stackpanel, or a stackpanel per control. I found that when I had several controls in a single stackpanel, it was only grabbing the first one, the rest of the controls were not getting generated. This would be a really nice feature that Microsoft could provide to all their controls because I know I am not the only person that has wanted this functionality but had to write this hack in get it accomplished.

How to bind an enumeration (enum) to a WPF/Xaml ComboBox

February 16th, 2012

You may already know this, but if you don’t I hope it is of some help to somebody out there looking to do this in their code. If you have an enum or enumeration and you simply want to have it as a choice in your UI and also want to bind it to a property on your View Model, I have attached the code to do this below, check it out.

public enum Enum {

        <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="odp">
                <x:Type TypeName="vm:Enum" />

<ComboBox ItemsSource="{Binding Source={StaticResource odp}}" IsSynchronizedWithCurrentItem="true" SelectedItem="{Binding PropertyToBindTo, Mode=TwoWay}" Width="100"/>

I am using a UserControl in my example but a standard Xaml window would work as well. Notice also that the key is named as so (x:Key=”odp”) you use that down in your combobox as a static resource since we are placing this in the resources of the user control. If you make a property with the enum as its type you can bind right to the SelectedItem just make sure that you set it to Mode=TwoWay or it may not work. The (ObjectType=”{x:Type sys:Enum}”) simply tells wpf that you are presenting it with an enum type that uses the ‘sys’ namespace. In This line ‘‘ the ‘vm’ is simply the namespace that your Enum sits. I hope this can help somebody out there wanting to implement this type of thing.