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

  • SEARCH
  • Home
  • 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 8531189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:25:36+00:00 2026-06-11T09:25:36+00:00

I have an abstract generic class that defines a generic dependency property the type

  • 0

I have an abstract generic class that defines a generic dependency property the type of which is to be defined by subclassing classes. This property is somehow not recognized as a dependency property so that I get an error message during runtime when binding to this property. Additionally during compilation time the constructor cannot call InitializeComponent. Why is that?

The generic abstract class MyClass:

abstract public class MyClass<T,U> : UserControl {
    protected MyClass() {
        InitializeComponent(); // Here is one error: Cannot be found
    }

    abstract protected U ListSource;

    private static void DPChanged
    (DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var myClassObj = (MyClass) d;
        myClassObj.DataContext = myClassObj.ListSource;
    }

    // Causes a binding error at runtime => DP (of the concrete subclass)
    // is not recognized as a dependency property 
    public static readonly DependencyProperty DPProperty = 
        DependencyProperty.Register(
        "DP", 
        typeof(T), 
        typeof(MyClass), 
        new PropertyMetadata(null, DPChanged));

    public T DP {
        get { return (T) GetValue(DPProperty); }
        set { SetValue(DPProperty, value); }
    }
}

The corresponding XAML:

<UserControl x:Class="Path.of.Namespace.MyClass"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ListView>
        <!-- Some stuff for the list view - used for all subclasses -->
    </ListView>
</UserControl>

A concrete subclass MySubClass:

public partial class MySubClass : MyClass<ClassWithAList, List<int>> {
    public MySubClass() {
        InitializeComponent(); // Another error: Cannot be found
    }

    protected List<int> ListSource {
        get { return new List<int>(); } // Just a dummy value

    }
}

The corresponding XAML:

<local:MySubClass xmlns:local="Path.of.Namespace.MySubClass" /> 

P.S. I am also not quite sure if the partial stuff is done correctly – R# suggests to remove these keywords.

  • 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-06-11T09:25:38+00:00Added an answer on June 11, 2026 at 9:25 am

    I think there are several things wrong with your code, but first, let me explain partial in this context:

    partial is used to declare one class in separate files. As visual studio is creating a MySubClass.g.cs with a partial class MySubClass (btw. this is where the method InitializeComponent is declared) from your MySubClass.xaml you’d have this class declaration twice and thus to get this to compile you will need the partial keyword.

    Now to the rest: XAML and generics don’t do well together, meaning: not at all. your ‘corresponding XAML’ is declaring a MyClass. But a MyClass without generic parameters doesn’t exist. You could at this point change x:Class="Path.of.Namespace.MyClass" to x:Class="Path.of.Namespace.MySubClass", that should work.

    but now the next thing is: how do you want to access that dependecy property in xaml? it is a static member in a generic type, so you have to specify it like this in code: MyClass<ClassWithAList, List<int>>.DPProperty. The problem is again: you can’t do that in xaml

    to solve your problem it might be a good idea to tell us what you want to accomplish

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

Sidebar

Related Questions

Suppose I have a Generic abstract class that provides some default constructor functionality so
I have confusing situation. Base Generic Type and successor public abstract class BaseType<TEntity> :
I am working on a project, and I have a generic abstract type that
I have a base class that has an abstract method which returns a list
I have an abstract generic view-model that I use as a base-class for several
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
Let say I have abstract class called: Tenant and Customer. The tenant in this
I have defined an abstract method in my base class to take in a
I have a c# data class defined with an inheritance chain that looks like
I have those classes, an implementation of active record pattern: public abstract class RecordCollection<T>

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.