Bruce VB111480

Hey,

I have created a call that inherits from button like this with no xaml.

public class ButtonBack : System.Windows.Controls.Button

{

}

I want to add some non visual properties to the class but inherit the standard themes if they are changed.

using this type of approach

Uri uri = new Uri("PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component\\themes/Royale.normalcolor.xaml", UriKind.Relative);

Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

On a windows page the standard button changes theme but the inherited ButtonBack does not. Do I have to recreate each standard theme and manually apply it to the ButtonBack class using a style/theme Should it not just inherit the style from the button class unless a style is explicitly applied. I have tried the BasedOn in a style resource in the generic.xml but it does not seem to work.

Anyone have a snipit of code or am I on the wrong path.

Thanks in advance!!



Re: Windows Presentation Foundation (WPF) Inheriting Button / applying theme

Yi-Lun Luo - MSFT

Hello, you can try to remove the Setter on Template after set BaseOn to Button in generic.xaml:

<Style TargetType="{x:Type local:MyButton}" BasedOn="{StaticResource {x:Type Button}}">

<!--<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="{x:Type local:MyButton}">

<Border Background="{TemplateBinding Background}"

BorderBrush="{TemplateBinding BorderBrush}"

BorderThickness="{TemplateBinding BorderThickness}">

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>-->

</Style>

Note if you uncomment the Setter (like auto generated by Visual Studio), it will override the default style's Template.






Re: Windows Presentation Foundation (WPF) Inheriting Button / applying theme

Bruce VB

Hey thanks!!

I had done that but neeed a few extra items.

1) add the themes to my themes folder.

themes\Royale.normalcolor.xaml

themes\luna.normalcolor.xmal

2) In my code do this:

  • Application.Current.Resources.MergedDictionaries.Clear();
  • Uri uri = new Uri("PresentationFramework.Royale;V3.0.0.0;31bf3856ad364e35;component\\themes/Royale.normalcolor.xaml", UriKind.Relative);
  • Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);
  • uri = new Uri("themes/Royale.normalcolor.xaml", UriKind.Relative);
  • Application.Current.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

This applies the system theme then adds my theme extensions to the merged dictionary. .

My theme for royale.normalcolor.xaml now looks like this.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:cc="clr-namespace:NNBaseControls;assembly=NNBaseControls"

xmlns:local="clr-namespace:NNPShell">

<Style BasedOn="{StaticResource {x:Type Button}}"

TargetType="{x:Type cc:ButtonBaseW}">

</Style>

<Style TargetType="{x:Type local:BBTest}"

BasedOn="{StaticResource {x:Type Button}}">

</Style>

<Style BasedOn="{StaticResource {x:Type Button}}"

TargetType="{x:Type cc:ButtonBack}" >

</Style>

<Style TargetType="{x:Type cc:ButtonBarMain}"

BasedOn="{StaticResource {x:Type TabControl}}">

</Style>

</ResourceDictionary>

Very cool. Now my custom controls get all the system theme styling without all the work. The trick is that each custom control needs to be assigned a basedon even if the control is inherited from a standard system control.

Note tha I have controls from different assemblies. My first attempt was to assign the theme in the local assembly which does not work unless it is loaded specifically in main application.

Thanks for your help!!!

Bruce VB