Can I set a default style for a basic control in WPF?
Suppose I have the following base classes:
public abstract class View<T> : ContentControl where T : ViewModel { static View() { DefaultStyleKeyProperty.OverrideMetadata(typeof(View<T>), new FrameworkPropertyMetadata(typeof(View<T>))); } // Other properties, methods, etc in here } public abstract class ViewModel { // Other properties, methods, etc in here }
Then suppose I have two classes that inherit from these base classes:
public partial class TestView : View<TestViewModel> { public TestView() { InitializeComponent(); }
Now I want to specify a default style for a basic control that uses all of my derived controls:
<Style TargetType="{x:Type local:View`1}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:View`1}"> <Border Background="Magenta" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <StackPanel> <Button>Test</Button> <ContentPresenter ContentSource="Content" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
However, when I use my TestView control, I do not have the template layout applied (and therefore, any content that I can define in the XAML of my TestView control is not in the visual / logical tree).
I am basically trying to use the base view classes / viewmodel and apply a consistent look. This, of course, works in cases not related to a common basis. However, I need a secure connection type between view and viewmodel, so I can call methods on the viewmodel from everything that references the view (which, as I know, may not sit well with the way some people implemented MVVM) .
wpf
Brad leach
source share