WPF form inheritances issue - wpf

I have some questions concerning the issue of form inheritance in WPF.
I have read that there is no visual inheritance in WPF Forms.
I would like to write my project as a base form, with other forms inheriting from it.
Some possible solutions were to use UserControl, and use it inside the son form.
The problem is that I have to define it again and again in every new form that inherits the base one.
Can I implement it another way, without defining it in the son-form?

You can use a style to handle this with less markup than a UserControl (you just have to set Style="{StaticResource myWindowStyle}" in every Window element). Using that style you can override the Template of a window, putting whatever chrome you like around the window content. Add a ContentControl with Content="{TemplateBinding Content}" wherever you want your sepcific Window's content to appear.
Unfortunately you can't apply this to all Windows easily, because setting a generic style will apply only to the base Window control, and each of your application's windows will be of a type that derives from Window.
EDIT: If you want to specify templated controls in your Window, you can do something like the following with your style:
<Style x:Key="MyWindowStyle" TargetType="Window">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Window">
<StackPanel>
<Button>This button will appear on every window</Button>
<ContentControl Content="{TemplateBinding Content}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Related

Similar WPF Windows

I have WPF Application and 2 Windows with similar structure (everything is same:menu, Title, Toolbar, only in the middle one stack panel is other, in one window has 10 TextFields,Button and other controls,and other one has other controls in this stack panel.I can copy one window in other, but I duplicate code.How can i solve this problem?Thanks.
One way would be to create a ControlTemplate and/or Style for the Window.
<ControlTemplate x:Key="MyWindowTemplate" TargetType="Window">
<AdornerDecorator>
<DockPanel>
<!-- Your toolbar content and other shared content -->
<!-- The dynamic content is loaded into the content presenter: -->
<ContentPresenter Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}" />
</DockPanel>
</AdornerDecorator>
</ControlTemplate>
<Style x:Key="MyWindowStyle" TargetType="{x:Type Window}" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="Template" Value="{StaticResource MyWindowTemplate}" />
</Style>
When you now create a window instance, you apply the style to the window and add some content into the content property and (if necessary) some ContentTemplate.
Create just one window and:
Add all the common controls required for both the screens.
Add the different stack panels in the same place and bind their visibility to the same flag.
Implement the InverseBooleanConverter and bind that to one of the stack panel's visibility based on how you are going to handle the flag.
Handle the flag before launching the view i.e. set it to true or false based on which window you are showing at that time.

WPF TreeView style template resetting IsExpanded on style switch

I have a basic TreeView on a window that has a style applied from a resource in a dll. The style dll is capable of switching between two styles. When I don't have a style for the TreeView in the dll I am able to expand the TreeViewItems, switch styles (of other controls), and the TreeViewItems remain expanded. However, as soon as I add a style for the TreeView, the TreeViewItems that have been expanded collapse as soon as the style is switched.
All fancy animations and TreeViewItem styling has been removed in order to track down the problem. The remaining style is simply:
<Style x:Key="{x:Type TreeView}" TargetType="{x:Type TreeView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeView">
<Border Name="Border"
Background="Transparent"
BorderThickness="1"
CornerRadius="1">
<ItemsPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Any suggestions would be greatly appreciated. I've ran out of ideas!
When you take away a template or replace by another some dependency properties will reset to their default value. The reason for that is dependency property precedence. Take a look at this here: http://msdn.microsoft.com/library/ms743230
Think of it like this: A dependency property may contain many values which are layered. The value in top most layer is always the current value. If you take a style away, you take layers away too. If you take all layers away, the dependency property will take default value as current value.
When you replace the style of your TreeView, all underneath styles will be updated/reinitalized/changed/resetted...
To fix this try keep same template and only change colors, borders and stuff like that.. or use Binding

Reset ControlTemplate

I'm using some fancy WPF-based UI framework that defines ControlTemplates for all the basic controls. So if I have a ListBox, it is styled according to the theme of this framework.
I'd like to use a single ListBox that has a completely different style, and so I'd like to disable the ControlTemplate for this particular control only and build up a style from scratch.
I've tried setting the Template property to null on this control, as shown below, but it didn't work:
<Setter Property="Template" Value="{x:Null}" />
How can I reset the ControlTemplate for this control in order to get rid of the framework-specific styles and weave my own?
No need to resort to styles and setters if you only want to change the template for this one control:
<TextBox>
<TextBox.Template>
<ControlTemplate>
<Rectangle Width="200" Height="20" Fill="Red"/>
</ControlTemplate>
</TextBox.Template>
</TextBox>

Why is my XAML control losing properties when applying a style template to it?

I'm trying to style this button, and I even though I state the height it should be in my XAML, the template in the styling seems to get rid of it.
Note: I am aware I can just have a style with no template, but I need the template because I'm using multiple themes / style files.
Control:
<Button Style="{DynamicResource basicButton}"
Height="50">
<Canvas...>
<Path... />
</Canvas>
</Button>
Styling:
<Style x:Key="basicButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="{DynamicResource solid_single_mainColor}">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Is this expected behavior? I intent to re-use this button style, so I need to be able to give it a height in the XAML. What am I missing? How can I give each new button I create a different height?
The problem is likely coming from the Canvas that you are placing inside the Button. In many cases a Canvas will have unbounded size unless you explicitly clip it. The Height of the Button itself isn't affected by its template as layout measurement will use the explicit value you have set but you may not be able to tell visually what size the Button itself is.

Styling a control template in Silverlight 5

I'm looking at using the Silverlight Menu Control from Codeplex. It's functionally very good but I'd like to restyle the appearance. Is it possible to do this without just making a new copy of the template and changing a few properties? Here's an example from the generic "theme" that comes with the control:
<Style TargetType="GenericControl:MenuBar">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="GenericControl:MenuBar">
<Canvas x:Name="LayoutRootMB">
<StackPanel Height="25"
x:Name="baseRectMB"
Canvas.Left="0"
Canvas.Top="0"
HorizontalAlignment="Left"
Orientation="Horizontal" />
</Canvas>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'd like to change, say, HorizontalAlignment to Stretch (for the sake of argument -- never mind if it'll actually accomplish anything useful). Is there any way to tell XAML (in XAML, not in C#) that I want to do something to baseRectMB?
baseRectMB is named in a TemplatePartAttribute on class MenuBar.
I assume I can simply copy the existing template and alter it, though I haven't gotten that working yet either.

Resources