Delay's Blog is the blog of David Anson, a Microsoft developer who works with the Silverlight, WPF, Windows Phone, and web platforms.
http://dlaa.me/
@DavidAns
One scenario I've seen cause a bit of trouble on the Silverlight Controls Forum is that of putting a Button in a ListBox. There are two aspects of this that seem to cause difficulty and I thought it would be helpful to demonstrate the complete scenario in a runnable, self-contained sample. (Please Download the ZIP file attached to the bottom of this post for all the code/XAML in a ready-to-go Visual Studio 2008 + Silverlight Tools solution.) While I was developing the sample, I threw in a couple of other handy techniques that may not be widely known. The sample application shows a typical shopping cart experience where products are listed and their quantities can be interactively changed:
Details on the button scenario:
Release
Press
<Button ... ClickMode="Press" ... />
<Button ... Click="Add_Click" ... />
Other points of interest:
Product
For lots more about configuring and using ListBox, please see my ListBox/ScrollViewer FAQ.
Quantity
public int Quantity { get { return _quantity; } set { _quantity = value; // Fire PropertyChanged event to notify listeners of changed value var handler = PropertyChanged; if (null != handler) { handler.Invoke(this, new PropertyChangedEventArgs("Quantity")); } } } private int _quantity; public event PropertyChangedEventHandler PropertyChanged;
bool
int
// Simple IValueConverter returns true iff the value is positive // Used to toggle Remove button's IsEnabled when Quantity changes between 0 and 1 public class IntIsPositive : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (0 < ((int)value)); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
At the risk of straying too far from the original scenario, I think the additional techniques I've shown here support it nicely and improve the user experience notably. I hope this sample helps people with their own projects - and maybe introduces a useful trick or two!
Enjoy!