Skip to main content

Posts

Showing posts with the label Beginner

C# Silverlight - Using Sliders [Beginner]

Being the month of Silverlight, I decided to get off my PHP and JS butt, and learn something new. I am incredibly new to the whole silvery-light thing, but luckily with Visual Studio on your side, it is pretty easy to get things going. I am also using VS2010, which makes the whole experience a grand opening of new stuff for me. So to begin my foray into Silverlight, I did something easy, but slightly useful at the same time. What I have made is a set of three sliders that represent a RGB value, which in turn changes the color of some text. It is some pretty short code, but it does showcase how to change the color of TextBlock text, which took me a few minutes to find. [silverlight width="400" height="300" src="SilverlightApplication.xap" border="true"] To begin with, lets get the XAML out of the way: <UserControl x:Class = "SilverLightColorSliders.Page" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml...

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...

C# WPF Tutorial - WCF Callbacks Hanging [Beginner]

Here's a bug that has driven me nuts over the past few days. I have a WPF application that communicates with a pretty basic WCF service. Whenever a callback is issued in the middle of a request, the WPF application completely hangs. It's obviously a synchronization issue, however I've gone through the forums and articles and set every imaginable attribute on every imaginable object with no successful outcome. In the end, the answer was unexpectedly simple, however not one I would have ever guessed. I accidentally stumbled across the solution during a round of trying totally crazy things in an attempt to make something work. All I had to do was create my channel on a thread other than the application's main thread. Let me demonstrate with some examples. public partial class Window1 : Window { IStringReverser _channel; public Window1() { InitializeComponent(); Callbacks callbacks = new Callbacks(_btnReverse); var factory = new DuplexChannel...

C# WPF Tutorial - Printing [Beginner]

Printing. Ugh. Every programmer I know hates writing code to do printing. Stuff never seems to appear quite how it should - the transition from the screen to the page can often be a very messy one. Printing with the Win32 API is ugly - something more akin to black magic and dark incantations than actual computer code. The .NET framework made it a little bit better in WinForms, but it was still just a very thin wrapper around the Win32 api - and it was still in the world of GDI. But now we are in the land of WPF! The land of lollipops and hope and magical wonders! So everything should be awesome, right? Well, actually, in many ways it is. It is a lot easier to just step right up and do some printing - and in all likelihood what you see on the screen will match what comes out on paper. This is due to two major factors. The obvious one is that they made a nicer API - it is a much thicker wrapper around the Win32 black magic than what WinForms had. The more subtle factor is the fa...

C# WPF Tutorial - The BasedOn Style Property [Beginner]

By this point, everybody knows and (mostly) loves styles in WPF. They give you the ability to customize and control from a high level - letting you abstract out the look of a control from the actual instantiation of a control. At this point I don't know how many times I've heard WPF styles referred to as "CSS, but for your application!" - and in many ways, that statement is right (although the cascading bit in WPF doesn't always work as well as CSS). Today we are going to take a look at a property of styles that can give you even more power to abstract away how your application looks - the BasedOn Property. The BasedOn property of a style lets you essentially do style inheritance (something I often wish CSS let you do in a much less cumbersome manner). It lets you "base" a style off of another style - you get all the settings of the base style, and then you add to them or override them as you see fit. This is great for when you have an overall ...

C# WPF Snippet - Detecting Binding Errors [Beginner]

Isn't it really annoying when WPF binding errors fail silently? The application compiles, the application runs, but nothing is working - all because of a silly typo in a binding somewhere. And then, once you realize it is a binding error (which is not always obvious), you have to drudge through the debug output trying to find that one line that says "System.Windows.Data Error ....". Even worse than that is the subtle binding error where the app still works, but maybe the text on some label isn't updating right, and you don't even notice the bug until weeks later. If you are as annoyed by all that as I am, you have come to the right place. Today we are going to take a look at a simple way to make binding errors literally explode in your face (as MessageBoxes!). We are going to take a look at adding a custom trace listener to listen for binding errors and pop them up as a message box when they happen. To do this, we have to create a custom trace listene...

C# WPF Snippet - Disabling Dragging from a TextBox [Beginner]

Reading the title, you are probably even wondering why this even deserves a tutorial. Disabling the ability to drag selected text out of a TextBox ? That has got to just be a flag somewhere, right? That is what I thought, and it turns out I was quite wrong. The TextBox in WPF has a lot of built in behavior, from responding to keyboard shortcuts for copy/paste to having its own context menu. It even has a built in spell checker and undo stack! One of these built in features is the ability to drag selected text into and out of a TextBox . Generally, this is probably a nice feature to have (although I don't ever use it) - but sometimes it can get in the way. For instance, if your application is paying close attention to mouse capture or focus (say you are inside a popup), an errant drag operation started from within your captured area doesn't generally do useful things. Disabling dropping into a TextBox is pretty easy - all you have to do is set the AllowDrop prope...