Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 970325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:47:14+00:00 2026-05-16T02:47:14+00:00

In a Silverlight project, I’ve got the following class defined. public class ThemeResource<T> {

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:47:15+00:00Added an answer on May 16, 2026 at 2:47 am

    You can’t. The usual solution would be something like this:

    public interface IThemeResource
    {
        public string Name { get; }
        public string Description { get; }
        public string TypeName { get; }
    }
    
    public class ThemeResource<T> : IThemeResource
    {
        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]; } }
    }
    

    Then you can pass around IThemeResources and get their properties regardless of their concrete generic type.

    (Perhaps your problem admits some other, simpler solution than using generic types for this at all, but I’ll leave that for someone else to come up with.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to run a Silverlight project in Visual Studios 2008 I get
The directory structure of my Silverlight project is like the following: \Bin - MainModule.xap
Project Structure I have a silverlight project SLProj, that references a silverlight class library
I created a RoleService in my silverlight project and through that got hold of
I made a Silverlight project like this: <UserControl x:Class=silverlighttest3.MainPage xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:d=http://schemas.microsoft.com/expression/blend/2008 xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 mc:Ignorable=d
I have a Silverlight project which takes some encrypted string thru its Service Reference:
I have a simple Silverlight project that is just getting a set of entities
I have a silverlight application which has a class library project in it. th
If I have a silverlight project and silverlight class library. In the class library
i've got a silverlight project which uses linq to sql classes and sql server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.