Archive for the 'Programming' Category

WPF: How to set the SelectedValue on Data Bound ComboBox

Most of the time when you have a form, you will have some combo boxes, and some list boxes. If you are editing an existing item, most likely you are going to want to have these items display the currently selected value. So we are going to populate a form with some contacts and provide a combo box of companies that they could work for. When we select an individual contact from our list, we would expect the combo box to change and show the company the contact currently is associated with.

Read more »

WPF Binding from the CornerRadius of a Border

Ok, after posing this question over at .NET Blog it took me all of about 30 seconds to realize what I needed to do.

Is it possible to do Binding on separate corners of the CornerRadius. I would like to have a slider that the top corners on the Border increase/decrease radius as the slider is moved. But I don’t want the bottom to corners to be affected.

I had used the tutorial over at Martin Grayson: Adventures of a ‘Designer’ to create a glass button for a project I am working on. This was a great starting point for my buttons, but there were a few things I needed to modify. I am only going to show you how I was able to bind to a slider to change the radius of the button.

The first thing I did was create the control template, notice though that where the corner radius is, I have binded it to a slider that I have added. But on the one corner, the corners originally were set to 4,4,0,0, which will mean that if we use the value from the slider, it will end up looking looking distorted like Figure 1.

Distorted Buttons
Figure 1

Read more »

WPF IDataErrorInfo and DataBinding

This is by far the best example of IDataErrorInfo and DataBinding that any beginner can easily grasp.

read more | digg story

How to create a generic method to get selected value from WPF ListBox Control

On a recent project, I had several listbox controls and needed to pass values to a web service. Since all my listboxs used the same template, I wanted a generic method to handle the events.

Start by creating a new project named ListBoxDemo in Blend or Visual Studio. I use V# 2008 Express for my projects and prefer starting my projects there.

Add a new class file to your project and name it Contact.cs

using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;

namespace ListBoxDemo
{
    public class Contact : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void Notify(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        int pID;
        public int ID
        {
            get { return this.pID; }
            set
            {
        if (this.pID == value)
                    return;
            this.pID = value;
            Notify(”ID”);
            }
        }

        string pFirstName;
    public string FirstName
        {
            get { return this.pFirstName; }
            set
            {
                if (this.pFirstName == value)
                    return;
                this.pFirstName = value;
                Notify(”FirstName”);
            }
        }

        string pLastName;
        public string LastName
        {
            get { return this.pLastName; }
            set
            {
                if (this.pLastName == value)
                    return;
                this.pLastName = value;
                Notify(”LastName”);
            }
        }

        public string Name
        {
            get { return this.pFirstName + ” ” + this.pLastName; }
        }

        public Contact() { }

        public Contact(int Id, string FirstName, string LastName)
        {
            this.ID = Id;
            this.FirstName = FirstName;
            this.LastName = LastName;
        }
    }

    public class Contacts : ObservableCollection { }

    public class ContactsKeeper
    {
        Contacts pContacts = new Contacts();

        public Contacts Contacts
        {
            get { return pContacts; }
        }

        public ContactsKeeper()
        {
            // This will populate the control with data. You could use this
            // to load from a database or a webservice
            pContacts.Add(new Contact(1, “John”, “Smith”));
            pContacts.Add(new Contact(2, “Mike”, “Wallace”));
            pContacts.Add(new Contact(3, “Sally”, “Miller”));
            pContacts.Add(new Contact(4, “Albert”, “Einstein”));
        }
    }
}

Read more »

Outlook Navigation Pane in WPF

This is an awesome article. I have a project that I am working on and this really helped me nail down a few things.

http://dotnet.org.za/rudi/archive/2008/03/07/codeproject-article.aspx

Employee Contacts (Part 4) - Building the Business Object Layer with PHP

  1. Building the MySQL database.
  2. Building a Data Abstraction Layer with PHP
  3. Building the Data Access Layer with PHP
  4. Building the Business Object Layer with PHP
  5. Building the Presentation Layer with PHP & HTML

Part 1 of this series covered building the MySQL database. In Part2, we built the data abstraction layer. With Part 3, we built our data access layer. At the data access layer, we made generated the queries to execute through the data abstraction layer. Part 4 of our series will cover the business object layer.

With the business layer, often times it will seem like we are simply making calls to the data access layer. And yes, that is true but remember that we are planning for future growth. A little extra time now can save you hours later down the road, and lead to far less headaches. As you will see when we build our presentation level, we should never make a direct call to the data access layer, those calls should be handled through our business layer.

Read more »

C# StringBuilder Class

Have you ever had an extremely long string you needed to use in an ASP.NET web page? Or after it is displayed, have you ever wanted to view the source of the page to see what the string is outputting? Well, if you want to do this with a standard string, then you either get one long line string or you have to add line breaks where you want them. Personally, I don’t like adding the line breaks, as it is one more place I can have something break. Thankfully Microsoft has addressed this with the StringBuilder class, which I graciously use every time I have long strings to deal with. Read more »