Skip to main content

Posts

C# Silverlight - Using a DataGridTemplateColumn [Beginner]

Whenever you pit designers against developers, it always seems to be the developer that loses. It's very rare that controls like the Silverlight DataGrid are left alone - designers want little tweaks and polish to increase the user experience. This tutorial is going to illustrate how to use one of the most flexible solutions to theming a DataGrid - the DataGridTemplateColumn. We'll be touching very little on the basics of how to use the Silverlight DataGrid. If you're new to the control. The first thing we're going to do is build a default DataGrid without any styling. I created a class to hold some information about the SOTC authors and bound a collection of those to my DataGrid. [silverlight width="400" height="300" src="BasicDataGrid.xap"] using System.Collections.Generic; using System.Windows.Controls; namespace DataGridStyling { public partial class Page : UserControl { public Page() { InitializeComp...

C# Silverlight - Loading a Client Side Image [Beginner]

Since the RTW (release to web) of Silverlight 2 was just the other day, I decided to take a look and see if this problem still existed. And what do you know! Not only is it possible, it is downright easy. You can load images from any stream, which means it could be a file stream from isolated storage, a file that the user just chose in an Open File Dialog, or even over the web using stuff like OpenReadAsync (on the WebClient class. Below, you can see the small example app that shows this off - you can pick any image file on your computer, and Silverlight can display it right there for you. [silverlight width="400" height="300" border="true" src="Silverlight2ImageLoading.xap"] This is going to be a short tutorial, because really there isn't much code behind it. So let's jump straight into the XAML: <UserControl x:Class="Silverlight2ImageLoading.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xa...

C# WPF Tutorial - DataGrid Row Headers [Beginner]

When using Data Grids to present data, row headers can be valuable additions - especially if the grid contains more columns than can fit on the screen at one time. Row headers don't scroll horizontally with the rest of the content, which means users can use them to see the context of the data they're viewing. This tutorial will demonstrate how to create row headers using WPF's DataGrid control. The first thing we need is some data to put in our DataGrid. I'm going to use a very basic Movie class that contains some basic information about movies for this. /// <summary> /// Class that holds basic details for a movie. /// </summary> public class Movie { public string Title { get; set; } public int Year { get; set; } public string Director { get; set; } }   Now let's create some of these and assign them to the DataGrid's ItemsSource. // Create a collection of movies. var movies = new List<Movie> { new Movie() { Title...

C# WPF Snippet - Binding a DataTable to a DataGrid [Beginner]

This is something that I expected to be straightforward - binding a DataTable to a WPF DataGrid. The problem is that DataTable doesn't implement IEnumerable, and unfortunately that's what the DataGrid's ItemsSourceproperty requires. Fortunately for us, there's a really simple workaround. DataTable has a property named DefaultView , which returns a DataView that can be used for binding purposes. // Create a DataTable and populate it with // some example data. DataTable myDataTable = new DataTable(); // Add columns to DataTable. myDataTable.Columns.Add("Column A"); myDataTable.Columns.Add("Column B"); // Add some rows to the DataTable. myDataTable.Rows.Add("A1", "B1"); myDataTable.Rows.Add("A2", "B2"); myDataTable.Rows.Add("A3", "B3"); // Bind DataTable to DataGrid. myDataGrid.ItemsSource = myDataTable.DefaultView;   The original purpose for the DataView is for custom sorti...

C# WPF Tutorial - UseLayoutRounding Property [Beginner]

Ever since the introduction of WPF, applications developed using the technology have all had a similar look - fuzzy. In .NET 4, the developers at Microsoft made great strides in the clarity and readability of WPF applications. Up until .NET 4, developers have used many tricks to get icons and line edges clearer than they are by default. Whereas some tricks may still be needed, one new property puts an end to much of the fuzziness frustration - UseLayoutRounding . Below is an example of how UseLayoutRounding can help us. The left image is how WPF acts by default. The right is with UseLayoutRounding set to true. The above examples are made using a very basic WPF application. The only difference between the applications is whether or not UseLayoutRounding is enabled. <Window x:Class="UseLayoutRoundingTutorial.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winf...

C# WPF Tutorial - BitmapScalingMode Quality [Beginner]

If you've recently upgraded a .NET 3.5 WPF application to .NET 4.0, you may have noticed that some of your images look like crap. This is because Microsoft has changed how WPF rescales images. Instead of the original high quality, it has been lowered to low quality. This was done, they say, to improve performance. Here's some images that demonstrate what's going on. I created an application and did nothing except change the target version of the .NET framework. .NET 3.5 is on the left and .NET 4.0 is on the right.   There's no way to change the default scaling mode in WPF, however you can put a style in your App.xaml or some other high-level place and have it apply to all of your images. <Window.Resources> <Style TargetType="{x:Type Image}"> <Setter Property="RenderOptions.BitmapScalingMode" Value="HighQuality" /> </Style> </Window.Resources>   I hav...

C# WPF Tutorial - Using the WPF Toolkit DataGrid [Beginner]

WPF comes with a large number of built in controls, but from the beginning it has lacked something that many application developers find extremely important - a DataGrid. You can use the ListView to create something approximating a DataGrid (I've talked about it in a this tutorial ), but it is a lot of work and not particularly straightforward. Thankfully, Microsoft realizes how important a full-featured DataGrid is - and how you probably don't want to wait for the next version of WPF to be able to use one. This is where the WPFToolkit comes in. The WPF Toolkit is "a collection of WPF features and components that are being made available outside of the normal .NET Framework ship cycle" which to me translates as "handy new controls I don't have to wait for". The WPF Toolkit has a couple different controls, but the big one is the DataGrid - and that is what we will be exploring today. You can grab the toolkit from here . It is an MSI install packa...