private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Button element = d as Button; FrameworkElement oldElement = e.OldValue as FrameworkElement; if (oldElement != null) { element.RemoveLogicalChild(oldElement); } FrameworkElement newElement = e.NewValue as FrameworkElement; if (newElement != null) { element.AddLogicalChild(newElement); } }