Inheritance in generic winforms controls - winforms

Is it possible to have winforms designer working for Control2 for the following scenario?
public class Control1 <T> : UserControl {}
public class Control2 <T> : Control1<T> {}

The designer cannot work with a generic type as it does not any idea which TYPE to put in for 'T'. You will need to create a concrete class for this to work:
public class StringControl2 : Control2<string> {}

Related

Generic ReactiveUserControl "cannot be edited in Design view"

I changed my UserControl to be a ReactiveUserControl and now I can't view the Design View. Is there anything I can do to get the designer to work with ReactiveUserControl?
The Visual Studio designer has issues when your control or window directly inherits from a generic class. This was a pretty common issue with WinForms as well. You can work around this issue by defining another non-generic class that sits between the generic ReactiveUserControl and your control:
public partial class MyUserControl : MyUserControlBase
{
public MyUserControl()
{
InitializeComponent();
}
}
public abstract class MyUserControlBase: ReactiveUserControl<MyUserControlViewModel>
{
}
In the XAML, our root object element is defined as the base element (MyUserControlBase) and its class declaration is connected to the partial class defined above (MyUserControl):
<myNameSpace:MyUserControlBase
x:Class="MyNameSpace.MyUserControl"
xmlns:myNameSpace="clr-namespace:MyNameSpace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

SimpleMVVM and Generic ViewModelBase

I recently found the SimpleMVVM toolkit and am trying to create a small example program. I am trying to create a CurrentViewModel parameter like so:
private ViewModelBase<> _CurrentViewModel;
public ViewModelBase<> CurrentViewModel
{
get { return _CurrentViewModel; }
set
{
_CurrentViewModel= value;
NotifyPropertyChanged(m => m.CurrentViewModel);
}
}
Any object referenced by CurrentViewModel will extend the SimpleMVVM ViewModelBase class like so:
public class HomeViewModel : ViewModelBase<HomeViewModel>
{ }
The problem I am having is that SimpleMVVM ViewModelBase requires a type T as an argument and I don't know how to create the parameter CurrentViewModel such that it can accept any ViewModel extending ViewModelBase.
One of the issues around using Generics '<T>' is that any consumer has to still know the type. If you consider adding an ICollection to your model, you have to know what it is a collection of so that you maintain strong typing.
The only exception is if you define a class which is itself generic, which can then pass on it#s type property to a child class. i.e.
CustomCollection<T>
{
ICollection<T> _foo;
}
To do what you're trying to do will require a seperate common interface that encapsulates the functionality you want from CommonViewModel.

CollectionViewSource as Class Property Type

I have a Class in my main WPF application which has a Property defined in the class as follows:
Public Class AppExample
Public PropertyName As CollectionViewSource
The project solution also inherits a Class Library (separate project but included in the solution and using the Inherits statement) - in the Class Library I want to do the same thing but I get an error.
Public Class ClassLibraryExample
Public PropertyName as CollectionViewSource
this results in:
Type 'CollectionViewSource' is not defined
How do I fix this?
Add the refernce of PresentationFramework.dll to your class library. It has namespace System.Windows.Data which contains CollectionViewSource
When you are using CollectionViewSource you have to use Data namespace(System.Windows.Data).

Using generic collections in Silverlight user controls

There is an interface:
public interface IFoo {
}
A Silverlight user control has a collection of IFoo instances:
public ObservableCollection<IFoo> Items { get; set; }
There is an abstract class that implements the interface:
abstract public class Foo : IFoo {}
And a class that further derives from that:
public class DerivedFoo : Foo {}
With all of that said, I'm trying to add instances of DerivedFoo into the control's collection via XAML, but I receive an error that DerivedFoo is not of type IFoo and cannot be used in the generic collection.
I did find a post in a forum that said this was a bug in Silverlight 3 but would be fixed (I am using Silverlight 4). Is this still a bug or am I going about this incorrectly?
Update:
My code is at home and I'm at work so I can't post the actual XAML, but from memory it was along the lines of:
<my:Thing>
<my:Thing.Items>
<my:DerivedFoo ... />
</my:Thing.Items>
</my:Thing>
The answer is...
The CollectionChanged event handler for the generic collection made an improper cast during the Add action.

wpf user control base class problem

I am new to WPF and have created a WPF User Control Library
I added a Base class that looks like this
public class TControl : UserControl
{
}
and want all of my controls to inherit from it.
I have a Control called Notification which looks like
public partial class Notification : TControl
{
public Notification()
{
InitializeComponent();
}
Works fine except when ever i recompile the hidden partial class where InitializeComponent() is defined gets regenerated and inherits from System.Windows.Controls.UserControl
this gives me an
Partial declarations of 'Twac.RealBoss.UserControls.Notification' must not specify different base classes
error,
is there anyway to force the generated class to inherit from my base class?
Your XAML file probably has:
<UserControl x:Class="YourNamespace.Notification" .... >
Try changing this to:
<Whatever:TControl x:Class="YourNamespace.Notification" xmlns:Whatever="clr-namespace:YourNamespace" />
The error you are getting is because the use of UserControl in the XAML tells the compiler to produce a partial class inheriting from UserControl, instead of inheriting from your class.
You can completely remove the ": TControl":
public partial class Notification : TControl
{
}
and write:
public partial class Notification
{
}
instead, since the base class is defined in the XAML part, as Paul wrote.

Resources