Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Generic User Control WPF

I've trying to build a user control that will display the content of a Dictionary

the problem is i don't know the types for the key and value in the User control, i would know at the point i create the user control but C# doesn't seem to want to let me create a Generic User Control that would let me pass them in to a hosted dictionary

ie

public class MyCtrl<TKey,TValue> : UserControl
{
    private Dictionary<TKey,TValue>
}

because it references a generated file in the .\Obj\Debug\MyCtrl.g.i.cs which is read only

the only solution that is presenting itself to me is to create the class from scratch and not letting the designer handle any of the formatting is there a better possiblity?

to give a little background in about 8-10 places in my code i need the user to populate dictionaries of values and instead of building 8-10 controls that all do exactly the same thing but with different Types (in fact a lot of the time the only difference is which enum is serving as the key) i wanted a single control to handle this

like image 649
MikeT Avatar asked Mar 15 '11 10:03

MikeT


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.


2 Answers

You can use generic as if you don’t use XAML. But if you want to use XAML to define your control, you can’t use generic

like image 69
Stecya Avatar answered Sep 30 '22 00:09

Stecya


I finally have a working answer.

I build the Control around a Hashtable that uses objects

then add an extension to the object class

    public static bool TryParse<TType>(this object obj, out TType result) 
    {
        try
        {
            result = (TType)Convert.ChangeType(obj, typeof(TType));
            return true;
        }
        catch
        {
            result = default(TType);
            return false;
        }
    }
    public static TType Parse<TType>(this object obj) where TType : struct
    {
        try
        {
            return (TType)Convert.ChangeType(obj, typeof(TType));
        }
        catch
        {
            throw new InvalidCastException("Cant cast object to " + typeof(TType).Name);
        }
    }

then add a generic property that calls the extension to cast the objects to a Dictionary

like image 20
MikeT Avatar answered Sep 30 '22 01:09

MikeT