How to define TextBox input restrictions?

I’ve done this in the past with an attached behavior, which can be used like this: <TextBox b:Masking.Mask=”^\p{Lu}*$”/> The attached behavior code looks like this: /// <summary> /// Provides masking behavior for any <see cref=”TextBox”/>. /// </summary> public static class Masking { private static readonly DependencyPropertyKey _maskExpressionPropertyKey = DependencyProperty.RegisterAttachedReadOnly(“MaskExpression”, typeof(Regex), typeof(Masking), new FrameworkPropertyMetadata()); /// <summary> … Read more

In WPF, how can I determine whether a control is visible to the user?

You can use this little helper function I just wrote that will check if an element is visible for the user, in a given container. The function returns true if the element is partly visible. If you want to check if it’s fully visible, replace the last line by rect.Contains(bounds). private bool IsUserVisible(FrameworkElement element, FrameworkElement … Read more

How to get children of a WPF container by type?

This extension method will search recursively for child elements of the desired type: public static T GetChildOfType<T>(this DependencyObject depObj) where T : DependencyObject { if (depObj == null) return null; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); var result = (child as T) ?? GetChildOfType<T>(child); if (result … Read more

How to bind to a PasswordBox in MVVM

Maybe I am missing something, but it seems like most of these solutions overcomplicate things and do away with secure practices. This method does not violate the MVVM pattern and maintains complete security. Yes, technically it is code behind, but it is nothing more than a “special case” binding. The ViewModel still has no knowledge … Read more