This is a migrated thread and some comments may be shown as answers.

Best practices to override Styles

4 Answers 1735 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Cedric
Top achievements
Rank 1
Cedric asked on 05 Feb 2018, 10:51 AM

Hi,

I have some questions to know the best practice to reuse styles overriding theme's ones.

My solution is organized as this:

    - The main exe is in Smag.MSH.csproj

    - My custom controls are in Smag.UI.Wpf

   - All the styles are defined in Smag.Styles.csproj

I'm using implicit themes, so Smag.Style embeds:

  - xaml files from Windows 8 theme for all used controls

  - xaml files overriding default themes for some controls
   eg: in MyRadGridView.xaml

      <Style x:Key="NoBorderGridView" BasedOn="{StaticResource {x:Type telerik:RadGridView}}" TargetType="{x:Type telerik:RadGridView}">

 

The main exe merge the theme's and my own resources like this:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <!--  telerik all the corresponding xaml are included here  -->
      <ResourceDictionary Source="pack://application:,,,/Smag.MSH.Style;component/Telerik/System.Windows.xaml" />
      <ResourceDictionary Source="pack://application:,,,/Smag.MSH.Style;component/Telerik/Telerik.Windows.Controls.GridView.xaml" />

     <!--  The real list is cut for brievity  -->

      <!--  Own controls styles  -->
      <ResourceDictionary Source="pack://application:,,,/Smag.MSH.Style;component/ControlsStyles/MyRadGridView.xaml" />

 

What is the best pratices to reuse all the styles (theme's default one en those i have overrided) in Smag.UI.Wpf ?

The default style are currently not found:

<Style BasedOn="{StaticResource RadListBoxStyle}" TargetType="{x:Type local:ListBox}" />

Raise an error saying RadListBoxStyle is not found, but the right xaml is included in app.xaml

Event using this way, it doesn't work:

<Setter Property="Background" Value="{telerik:Windows8Resource ResourceKey={x:Static telerik:Windows8ResourceKey.MainBrush}}" />

 

As it's a DLL, there is no app.xaml to merge ressources, doing it in each control like this seems to me not being performant:

<ResourceDictionary xmlns="..." xmlns:x="..." xmlns:i="..." xmlns:local="clr-namespace:Smag.UI.Wpf.Controls" xmlns:telerik="...">
   <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/System.Windows.xaml" />
    <ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.xaml" />
    <ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Data.xaml" />
    <ResourceDictionary Source="/Telerik.Windows.Themes.Windows8;component/Themes/Telerik.Windows.Controls.Input.xaml" />
 </ResourceDictionary.MergedDictionaries></p>

 

Thanks for some more guidelines, the official documentation is not enough.

 

4 Answers, 1 is accepted

Sort by
0
Cedric
Top achievements
Rank 1
answered on 05 Feb 2018, 11:05 AM

Furthermore, i don't understand why i have to merge all the ressources one by one in my app.xaml like described previously instead of  using this:

<Application.Resources>
 <ResourceDictionary>
   <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/Smag.MSH.Style;component/Style.xaml" />

This throws an XamlParseException, resource RadNumericUpDownStyle not found which i guess is the first one consumed by the first window i show.

Style.xaml is in Samg.Styles.csproj, compiled as ressource and contains all the xaml to merge:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">    
<ResourceDictionary.MergedDictionaries>
 <!--  telerik  -->
<ResourceDictionary Source="Telerik/System.Windows.xaml" />
        <ResourceDictionary Source="Telerik/Telerik.Windows.Controls.Data.xaml" />
        <ResourceDictionary Source="Telerik/Telerik.Windows.Controls.GridView.xaml" />
        <ResourceDictionary Source="Telerik/Telerik.Windows.Controls.Input.xaml" />

 

0
Accepted
Sia
Telerik team
answered on 08 Feb 2018, 09:56 AM
Hello Cedric,

Straight to your questions:

1. The chosen strategy of first merging our resources, separating your custom styles per assembly  and merging them after ours is the best possible, because this makes upgrading and maintaining easier.  However I would not copy them locally but reference them from the Telerik theme binary if possible, as we recommend copying the XAML files only if you need to fully customize them locally.
2. Regarding the problem with nested resource dictionaries. Please keep your structure as flat as possible. In the following blog post there is a very good explanation of the reasons behind keeping flat structure. More information can be found also in the relevant StackOverflow thread. From the proposed solutions there I would choose one of these two:
- Define all merged dictionaries in a single hierarchy level in App.xaml of Smag.MSH.csproj
- Use more complex hierarchy and use DynamicResource where is needed.
3. Regarding the dll, unfortunately you will need to merge the needed resources in all controls. I will think about better solution and will let you know if something comes to my mind.

Regards,
Sia
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Cedric
Top achievements
Rank 1
answered on 08 Feb 2018, 01:52 PM
Thanks
0
Sia
Telerik team
answered on 09 Feb 2018, 01:28 PM
Hello,

I see that you have marked my answer and that is why I will close the ticket for now. You can always follow-up if you need additional help with styling the controls in your application.

Regards,
Sia
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
General Discussions
Asked by
Cedric
Top achievements
Rank 1
Answers by
Cedric
Top achievements
Rank 1
Sia
Telerik team
Share this question
or