Skip to main content

Posts

C# - Using WPF In WinForms [Beginner]

A little while back, we did a tutorial on how to embed WinFoms controls inside of WPF application (you can read all about it here ). Today, we are going to flip that on its head - we are going to take a look at how to embed WPF controls inside of a WinForms application. You might be thinking to yourself "why would someone want to do that?" A great question, and there are a couple different reasons. First, perhaps you have an application already written in WinForms, but you want to move to WPF. Instead of rewriting the entire thing from scratch (which might be a large amount of work, and might be time/cost prohibitive), you can rewrite portions of the application as time allows or when you need to redo a particular section anyway. Another reason would be if there is a area of your app that would be easy to do in WPF (maybe because of the animation features, or the composibility) while in WinForms it would be difficult. So what do we need to do to enable this i...

C# - Using WinForms In WPF [Beginner]

So we all know that WPF is awesome, but it is still a very young framework. And so, sometimes, it doesn't have everything that we might want or desire. For instance, there are a number of controls in WinForms that don't exist in WPF - and they come in handy once in a while. What we are going to look at today is how to use WinForms controls inside of WPF - a very easy and almost painless process, in my opinion. We are going to take a look at both embedding WinForms using C# code and using XAML - both have their own advantages and disadvantages. The WinForms control we are going to be using is the DataGridView - a powerful WinForms control that does not have a comparable WPF control (although, the service pack update to WPF coming this summer will include a DataGrid - possibly eliminating the need for embedding the WinForms version). There are some drawbacks for embedding, and we will address those as well. So the first thing that we need to do (after creating a new ...

C# - Introduction To Dependency Properties In WPF [Beginner]

So I was going to dive right in and do a part 2 on the WPF ListView tutorial recently, but as I was writing the code I realized that a lot of it relies on some new and very different constructs that WPF provides to developers. Two of these are deep enough topics on their own that I thought it would be a good idea to give an introduction to them before I dove back into the ListView stuff. So today we are going to talk about Dependency Properties, and in a future tutorial I will talk about how binding works in WPF. What are dependency properties? The quick definition from the MSDNdocs says that a dependency property is a "property that is backed by the WPF property system." Not really a great one-line explanation, but really, I can't blame them. I can't come up with a good one line explanation either. But essentially, it gives you a bunch of infrastructure to do all the things that you often want to do with a normal property - validate it, coerce it into a pr...

C# - Using The ListView, Part 1 In WPF [Beginner]

Many of the controls in WPF have a downright dizzying array of capabilities and features, due in large part to the composibility of all the components. The ListView control is a great example of this - the possibilities are almost endless. This series of tutorials on the ListView will hopefully make the space of possible options seem not quite as daunting. We will be starting today with a simple grid based list view, showing how to create columns and some different ways of getting data into those columns. This tutorial won't be very exciting - we will mostly just be setting up the basics for use in some of the later parts. Hopefully, the series will culminate in a sort of "DataGridView" for WPF (a control that, sadly, does not yet exist in WPF). I say hopefully cause I haven't yet written the code for those parts : OK, so here's a screenshot of a ListView with a few columns and no data. Nothing really fancy, but the basic starting point. The ...

C# - Creating Custom Shapes In WPF [Beginner]

I don't know if you've ever had to draw your own shapes before WPF, but if not, I can tell you it used to be a lot harder. You don't have to override the OnPaint function or listen for the Paint event anymore. You simply create a shape as if it were any other object and position it where you want it. WPF takes care of everything else. In order to play around with XAML and WPF, you should probably pick up a copy of either Visual Studio 2008 or Expression Blend 2. You can download any of the free Express Editions of Visual Studio right here . Let's start out with an easy example to get warmed up. One of my favorite games is Tetris, so here's a Tetris shape. Here's all the code required to make the above application. <Window x:Class = "XAMLCustomShapes.Window1" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" Title = ...