In a Silverlight project, I've got the following class defined.
public class ThemeResource<T>
{
public string Name { get; set; }
public string Description { get; set; }
public Type Type { get { return typeof(T); } }
public string TypeName { get { return Type.FullName; } }
public T Resource { get { return (T)Application.Current.Resources[this.Name]; } }
}
With several lists using the class in my code-behind.
public List<ThemeResource<SolidColorBrush>> BrushResources { get; set; }
public List<ThemeResource<Color>> ColorResources { get; set; }
public List<ThemeResource<FontFamily>> FontNameResources { get; set; }
public List<ThemeResource<Thickness>> ThicknessResources { get; set; }
public List<ThemeResource<Double>> FontSizeResources { get; set; }
public List<ThemeResource<Style>> TextStyleResources { get; set; }
And my view has several list boxes bound to the lists, like this.
<UserControl x:Name="ThemeResourcesPage" ...>
<ListBox Style="{StaticResource BrushResourceListBoxStyle}"
ItemsSource="{Binding ElementName=ThemeResourcesPage, Path=BrushResources}"
SelectionChanged="ListBox_SelectionChanged" />
</UserControl>
I want to have a single SelectionChanged event handler in my code-behind that all of my lists use to display details about the ThemeResource that was selected. My problem is that the SelectionChangedEventArgs' AddedItems is a list of objects, and the SelectedItem property on the ListBox is an object.
This won't work:
private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (e.AddedItems.Count != 1)
return;
var tr = (ThemeResource<T>)e.AddedItems[0];
var msg = string.Format("Name: {0}\nType: {1}\nDescription: {2}",
tr.Name, tr.TypeName, tr.Description);
MessageBox.Show(msg);
}
How do I cast the object to ThemeResource without knowing what T will be?