C# – How to find a control by name or type in the VisualTree

This function allows you to get a control by name and type in a given parent control VisualTree. Keep in mind that recursively searching a tree downwards could be a lengthy process. Note that if the argument name is undefined, the function returns the first found child element that matches the submitted type T. In … Continue reading “C# – How to find a control by name or type in the VisualTree”

C# – How to find the first parent element of a given child that matches the submitted generic type T

This static class allows you to get the first parent element of a given child element in the VisualTree that matches a submitted generic type. This last method “GetParentObject” is an alternative to WPF’s “VisualTreeHelper.GetParent” method, which also supports content elements. Do note, that for content element, this method falls back to the logical tree … Continue reading “C# – How to find the first parent element of a given child that matches the submitted generic type T”

C # – Understanding ‘in’ and ‘out’ (Generic Modifier)

in (Generic Modifier) For generic type parameters, the in keyword specifies that the type parameter is contravariant. You can use the in keyword in generic interfaces and delegates. Contravariance enables you to use a less derived type than that specified by the generic parameter. An interface that has a contravariant type parameter allows its methods … Continue reading “C # – Understanding ‘in’ and ‘out’ (Generic Modifier)”

C# – how to assign a default value to generic type var

In generic classes and methods, one issue that arises is how to assign a default value to a parameterized type T when you do not know the following in advance: Whether T will be a reference type or a value type. If T is a value type, whether it will be a numeric value or … Continue reading “C# – how to assign a default value to generic type var”