2

Why is this:

public abstract class WindowControls<T> : Window

not possible. I can't seem to figure it out.

    public partial class AnglesteelWindow : WindowControls<AngleSteel> {
    private UCListView uc;
    public AnglesteelWindow() {

        InitializeComponent();
        uc = new UCListView();
        uc.SubmitClick += new EventHandler(ButtonPressed);
        this.uc.grid.PreviewMouseLeftButtonUp +=
            new System.Windows.Input.MouseButtonEventHandler(
                 this.MousePressed14<AngleSteel>);
        stkTest.Children.Add(uc);
        uc.amountLabel.Content = "Milimeter";
        uc.grid.ItemsSource = DatabaseLogic.MaterialTable("Anglesteel").DefaultView;
        base.Material(uc, "Anglesteel");
    }
}

I know how generics work, but don't know why it is not possible to make my AnglesteelWindow derive from WindowControls. The error it gives me is the following:

Base class of 'Name of the solution' differs from declared in other parts.

When i look at the so called other part it is the following:

    public partial class AnglesteelWindow : 
          WindowControls<AngleSteel> System.Windows.Markup.IComponentConnector {

This is made in the AnglesteelWindow.g.i.cs file. If i remove it from there it makes no difference at all.

3
  • Does this window have a XAML file? Commented Nov 30, 2015 at 19:49
  • The problem is not in your C# code, but in it's incompatibility with the XAML file. Commented Nov 30, 2015 at 19:50
  • This question is similar to: How to specify generic type argument in XAML. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Mar 2 at 14:11

2 Answers 2

3

Adding on @MichaelMairegger answer, you can reach your goal by creating another non-generic class that inherits from the generic class like this:

public abstract class WindowControlsOfAngleSteel : WindowControls<AngleSteel>
{

}

And make your window class inherit from it like this:

From XAML:

<ns:WindowControlsOfAngleSteel >

</ns:WindowControlsOfAngleSteel >

Where ns is the namespace where WindowControlsOfAngleSteel exists.

In code (optional):

public partial class AnglesteelWindow : WindowControlsOfAngleSteel
{

}
Sign up to request clarification or add additional context in comments.

7 Comments

if you remove the abstract in the base class then the answer is correct.
@MichaelMairegger, why do you think that abstract needs to be removed? I actually tested this code.
Sorry, my mistake, I thought defining a abstract class as root results in an error. Have never tried that.
The thing is that this would be used for 6 windows which each have a different name but the same functionality, so i would make 6 WindowControlsOfAnglesteel, that seems kinda much :s
Do they all need to inherit from WindowControls<Anglesteel> or some other WindowControls<SomeThingelse>?
|
1

You cannot change the inheritance tree. AnglesteelWindow is partial because it is also declared in AnglesteelWindow.xaml where the root element is Window. If you want to inherit from another class you have to replace there the Window root by your base class.

public class MyDerivedBaseWindow : Window {}



<ns:MyDerivedBaseWindow >
    <!-- WindowContent-->
</ns:MyDerivedBaseWindow >

But you cannot use a Generic class in XAML. You have to change your logic that the base-window-class that you want to use as window-root is non-generic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.