Windows Forms is a Maintenance Risk
People say that WPF is a pretty big change from the Windows Forms technology of yesteryear, but there are also people who have invested considerable time and money into Windows Forms solutions that aren’t eager to rewrite their apps yet again. I can understand that—it takes time to develop things, and rewriting things for the sake of using the latest and greatest is not usually something that keeps projects on time and on budget.
But any new development should definitely be written in WPF.
Here’s a snippet of Windows Forms code that keeps a TextBox on the screen synchronized with a Quantity field on an Order object:
|
MyEditor.cs
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| public class MyEditor : UserControl
{
private System.Windows.Forms.TextBox quantityTextBox;
public MyEditor()
{
InitializeComponent();
}
private void InitializeComponent()
{
quantityTextBox = new System.Windows.Forms.TextBox();
quantityTextBox.TextChanged += new System.EventHandler(quantityTextBox_TextChanged);
// ...more auto-generated noise
}
public void SetOrder(Order order)
{
_order = order;
_order.PropertyChanged += order_PropertyChanged;
quantityTextBox.Text = _order.Quantity.ToString();
}
private void quantityTextBox_TextChanged(object sender, EventArgs e)
{
long qty;
if (long.TryParse(quantityTextBox.Text, out qty))
{
_order.Quantity = qty;
quantityTextBox.BackColor = Color.White;
}
else
{
quantityTextBox.BackColor = Color.Pink;
}
}
private void order_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Quantity")
{
quantityTextBox.Text = _order.Quantity.ToString();
}
}
} |
|
And in WPF:
| MyEditor.xaml |
1
2
3
4
5
| <UserControl x:Class="MyEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextBox Text="{Binding Path=Quantity}"/>
</UserControl> |
|
| MyEditor.xaml.cs |
1
2
3
4
5
6
7
8
9
10
11
12
| public partial class MyEditor
{
public MyEditor()
{
InitializeComponent();
}
public void SetOrder(Order order)
{
this.DataContext = order;
}
} |
|
Which one would you rather maintain? —DKT
Tags: .NET, MVVM, technology, WinForms, WPF, XAML
This entry was posted
on Monday, July 27th, 2009 at 10:55 PM EST and is filed under Programming.
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
我读了你写的文章.觉得你写得非常好.还有你对Windows窗体的解决方案的例子.等等…
希望在将来你可以写出更好的论点.
你忠实的读者
Thanks for the convincing argument… time to move over to the latest and greatest!
I tend to agree that WPF is more maintainable, but you could databind in Windows Forms too:
Binding binding = new Binding(“Text”, data, “FirstName”);
txtFirstName.DataBindings.Add(binding);
There’s even a pretty designer to help you with your bindings.
Here’s a tutorial:
http://www.codeproject.com/KB/database/databinding_tutorial.aspx
Indeed—but I haven’t ever seen anyone ever actually use it. Cumbersome, restrictive, and also crashy when it doesn’t work. I tried it once myself just to see what it took to use it, and it’s a far cry from what they gave us with WPF. It took me forever to diagnose an incorrect binding because all I got were cryptic exceptions instead of the (moderately) helpful errors that get spat out in a WPF app. I’m glad that Microsoft decided their Windows Forms binding solution wasn’t the end-all-be-all, because WPF binding is a true innovation. —DKT