I've been reading a few comments and questions floating around in the Silverlight ethos around DataBinding and basically the how. The most common examples I've seen usually show the Binding capabilities within XAML but don't really break open the "code-behind" or more to the point, "how to bind to a custom model with code only".
Here's how (think ShoppingCart as your example):
1: public class ShoppingCart : INotifyPropertyChanged, IModel
2: {
3:
4: string mycartname;
5:
6: public string MyCartName
7: {
8: get { return mycartname; }
9: set { mycartname = value; Notify("MyCartName"); }
10: }
11:
12: // boilerplate INotifyPropertyChanged code
13: void Notify(string mycartname)
14: {
15: if (PropertyChanged != null)
16: PropertyChanged(this, new PropertyChangedEventArgs(mycartname));
17: }
18:
19: public event PropertyChangedEventHandler PropertyChanged;
20:
21: }
1: public partial class ShoppingCart : UserControl
3: ShoppingCart sh;
4: public ShoppingCart()
5: {
6: InitializeComponent();
7: sh = new ShoppingCart();
8: Binding bnd = new Binding("MyCartName");
9: bnd.Mode = BindingMode.OneWay;
10: bnd.Source = sh;
11: txtData.SetBinding(TextBox.TextProperty, bnd);
12: sh.MyCartName = "I was added after Binding";
13:
14: // Bind the RoutedEvent to handle a button click to update the
15: // Model attribute only.
16: btnUpdateCart.Click += new RoutedEventHandler(btnUpdateCart_Click);
19: void btnUpdateCart_Click(object sender, RoutedEventArgs e)
20: {
21: sh.MyCartName = "I was added after a Click";
22: }
23:
24: }
That's it, DataBinding 101 with Code Only - Look mah, no XAML.
I'm going to explore more about DependencyProperty later next week as I think we haven't covered them off as well as we could, no biggy, it's still beta 2 so all is forgiven.
Any suggestions, feedback around DataBinding please drop me a comment or email and will be more than happy to discuss them.