public _ViewBase() >> { >> StackPanel sp = new StackPanel(); >> Button b = new Button(); >> b.Content = "Click me"; >> sp.Children.Add(b); >> this.Content = sp; >> }>>But I agree with John - from a WPF POV this is very unlikely to be the right approach. Are you using MVVM for this app?
namespace Junk { public class _ViewBase : UserControl { public Grid G { get; set; } public _ViewBase() { G = new Grid(); G.RowDefinitions.Add(new RowDefinition()); G.RowDefinitions.Add(new RowDefinition()); Button b = new Button(); b.Content = "Base Class Button"; b.SetValue(Grid.RowProperty, 0); G.Children.Add(b); this.Content = G; } public ContentControl AddedContent { get { return (ContentControl)GetValue(AddedContentProperty); } set { SetValue(AddedContentProperty, value); } } static readonly PropertyMetadata m = new FrameworkPropertyMetadata(null, SetContent); public static readonly DependencyProperty AddedContentProperty = DependencyProperty.Register("AddedContent", typeof(ContentControl), typeof(_ViewBase), m); private static void SetContent(DependencyObject d, DependencyPropertyChangedEventArgs e) { _ViewBase v = (_ViewBase)d; ContentControl c = (ContentControl)e.NewValue; c.SetValue(Grid.RowProperty, 1); ((_ViewBase)d).G.Children.Add(c); } } }Then
<Window x:Class="Junk.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Junk"> < Window.Resources > < ContentControl x:Key="Content" > < StackPanel > < Button >This is the added button< /Button > < /StackPanel > < /ContentControl > < /Window.Resources > < local:_ViewBase AddedContent="{StaticResource Content}" / > < /Window >Note to Michel (if you see this): The above XAML does not display correctly unless white space is added ??