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# – How to dinamically select a DataTemplate for each item in a list

Using a DataTemplateSelector, we can determine at runtime which DataTemplate to apply to show items in a ItemsControl derivated control like the GridView, ListView, ListBox, etc. For example, suppose we have a list of items of the below type: And we want to show either text or image content in the same list, based on … Continue reading “C# – How to dinamically select a DataTemplate for each item in a list”

C# – How to make items of ListView fill the entire list width

One thing that happened to me often was the items of an ItemsControl derivative (like a ListBox or ListView) not fill the full width of the list when was defined a DataTemplate for the ItemTemplate of the list. For example, the Xaml code below: … produces this: It should be noted that despite <Grid HorizontalAlignment=”Stretch”>, … Continue reading “C# – How to make items of ListView fill the entire list width”

C# – Implementing Finalize and Dispose to clean up resources

Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. To provide explicit control, implement the Dispose method provided by the IDisposable interface. The consumer of … Continue reading “C# – Implementing Finalize and Dispose to clean up resources”

Free pdf book with advices and tips for writing better code (c# and .net)

A good book with advices and tips for writing better code. Free download FoundationsOfProgramming.pdf Copyright © Karl Seguin About the Author Karl Seguin is a developer at Epocal Corporation, a former Microsoft MVP, a member of the influential CodeBetter.com community and an editor for DotNetSlackers.

C# – Throwing Exceptions best practices

Many people questioned themselves about which of the two forms is the best to (re) throw an exception? Is it better to do this : or this: The way to preserve the stack trace is through the use of the throw; This is valid as well throw ex; is basically like throwing an exception from … Continue reading “C# – Throwing Exceptions best practices”

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”