I was doing work where a lot of the functionality in an WPF MVVM project was within the
View. Part of the
View would change visibility, and when these parts again became visible, they needed to be back to a default value. With this behavior, this functionality can be implemented with a behavior attached to the control.
I had previously done a couple of tips that also reset values on visibility changed. They were behaviors that attacked the control containing a number of
CheckBox controls. On working on these behaviors, I realized that it may be desirable to be able to handle more types of controls, and to be able to set not just
Type values, but more general values.
Using the Code
Here is the code for the behaviour:
public class DefaultValueOnVisibilityBehavior
private static void OnDefaultValuePropertyChanged(DependencyObject d,
var element = (FrameworkElement)d;
element.IsVisibleChanged -= ElementOnIsVisibleChanged;
element.IsVisibleChanged += ElementOnIsVisibleChanged;
public static object GetDefaultValue(DependencyObject obj)
public static void SetDefaultValue(DependencyObject obj, object value)
public static readonly DependencyProperty DefaultValueProperty =
private static void ElementOnIsVisibleChanged(object sender,
FrameworkElement frameworkElement = (FrameworkElement)sender;
var defaultValue = GetDefaultValue(frameworkElement);
var defaultValueString = defaultValue.ToString();
var dependencyObject = (UIElement)sender;
if (sender is ToggleButton toggleBusson)
toggleBusson.IsChecked = defaultValueString.ToLower() == "true"
? true : defaultValueString.ToLower() == "false"
? false : (bool?)null;
else if (sender is TextBox textBox)
textBox.Text = defaultValueString;
else if (sender is ContentControl contentControl)
contentControl.Content = defaultValue;
There is a single
DefaultValue. This is the value that a
DependencyProperty on the control is set to. The changing of this
DependencyProperty will cause the
event handler to be assigned to the
event. When the
Visibility changes, then the value of the
DependencyProperty will be assigned to a
DependencyProperty of the control based on the control
Type. The following are the
DependencyProperty definitions that are affected:
ContentControl (other than above):
More can be defined, such as for
ListBox, and could add the functionality to select the
DependencyProperty that is affected, but I did not need this capability.
Using the Behavior
Here are some simple examples of how to use this behavior:
Content="RadioButton Two (Default true)" />
Content="ToggleButton Two (Default true)"/>
<RadioButton Grid.Row="3" Margin="5"
Content="RadioButton Four" />
local:DefaultValueOnVisibilityBehavior.DefaultValue="This is the default Value" />
- 05/02/2018: Initial version
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at email@example.com.