Update Controls for WPF

I've released Update Controls support for WPF. You can see a quick demo video that demonstrates building a WPF application from scratch using the new markup extension. It's better than data binding.

With this release, you can bind WPF controls directly to CLR properties without implementing INotifyPropertyChanged. All you do is use the Independent class to keep track of gets and sets. And the Visual Studio add-in will even do that for you when you select the properties and hit Ctrl+D, G.

public class Person
{
    private string _firstName;
    private string _lastName;
    private int _displayStrategy;

    #region Independent properties
    // Generated by Update Controls --------------------------------
    private Independent _indDisplayStrategy = new Independent();
    private Independent _indFirstName = new Independent();
    private Independent _indLastName = new Independent();

    public string FirstName
    {
        get { _indFirstName.OnGet(); return _firstName; }
        set { _indFirstName.OnSet(); _firstName = value; }
    }

    public string LastName
    {
        get { _indLastName.OnGet(); return _lastName; }
        set { _indLastName.OnSet(); _lastName = value; }
    }

    public int DisplayStrategy
    {
        get { _indDisplayStrategy.OnGet(); return _displayStrategy; }
        set { _indDisplayStrategy.OnSet(); _displayStrategy = value; }
    }
    // End generated code --------------------------------
    #endregion

    public string FirstLast
    {
        get { return FirstName + " " + LastName; }
    }

    public string LastFirst
    {
        get { return LastName + ", " + FirstName; }
    }

    public string Display
    {
        get { return DisplayStrategy == 0 ? FirstLast : LastFirst; }
    }
}

The FirstName, LastName, and DisplayStrategy properties are all independent. Their values are determined by the user. The FirstLast, LastFirst, and Display properties are all dependent. Their values are determined by code. Notice that there is absolutely no bookkeeping code around the dependent properties. Yet you can still bind to them using u:Update.

<Window x:Class="UpdateControls.XAML.Test.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:u="clr-namespace:UpdateControls.XAML;assembly=UpdateControls.XAML"
    Title="{u:Update Display}" Height="300" Width="300">
	<StackPanel>
		<StackPanel Orientation="Horizontal">
			<Label Content="First Name:" Width="100"/>
			<TextBox Text="{u:Update FirstName}" Width="170"/>
		</StackPanel>
		<StackPanel Orientation="Horizontal">
			<Label Content="Last Name:" Width="100"/>
			<TextBox Text="{u:Update LastName}" Width="170"/>
		</StackPanel>
		<StackPanel Orientation="Horizontal">
			<Label Content="Display As:" Width="100"/>
			<ComboBox SelectedIndex="{u:Update DisplayStrategy}" Width="170">
				<ComboBoxItem Content="{u:Update FirstLast}" />
				<ComboBoxItem Content="{u:Update LastFirst}"/>
			</ComboBox>
		</StackPanel>
	</StackPanel>
</Window>

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        DataContext = new Person();
    }
}

And, by the way, it still supports Winforms.

Download it and try it in your next WPF application. You'll never want to go back to plain-old data binding again.

Leave a Reply

You must be logged in to post a comment.