3

Is there a way to save a default value for dynamic ressources in a custom control library?

I have created a custom control library and therefore I use a default Style which resides in the Generic.xaml file. This "default style" uses references to dynamic resource markers (see in the example).

<Style TargetType="{x:Type local:BorderlessWindow}"> <Setter Property="Foreground" Value="{DynamicResource ForegroundColor}" /> <Setter Property="Background" Value="{DynamicResource BackgroundColor}" /> <Setter Property="TitleBackground" Value="{DynamicResource AltBackgroundColor}" /> <Setter Property="Template" Value="{StaticResource DefaultBoderlessWindowTemplate}" /> </Style> 

Everything is working as expected if I reference to my custom control library in a new project and add the dynamic resource markers in this new projects app.xaml but the values are empty if I do not do this.

So I want some kind of default values. In other words:

"Take the value of {DynamicResource ForegroundColor} or if this do not exist blue."

I thought I just have to add the default values in the Generic.xaml (MergedDictionary) but this wont do the job. Does anyone have a solution?

The only solution I can think about is to replace the dynamicResource markers with the concret (default) values (e.g. blue, green, black) and handle resources in the "consuming" app if you know what I mean.

3
  • Resources Follow the visual tree, so if you have a resource in your control then that is the resource that is used, if the resource is not present then it looks further up the tree for the resource, this means that if you define a application resource with the same criteria as your control resource it will be used as a default if your resource is not present Commented Feb 8, 2017 at 16:15
  • 1
    Those steps (add reference and add resource dictionary somehow) are must, if someone doesn't do them - it's his problem. You can add those into documentation (readme.txt) of library, but that's all what is needed. If you really need to give a hint, then you can create custom MarkupExtension used instead of DynamicResource, where you check for availability of resource at the moment when xaml is loaded/parsed, unfortunately I don't have time to quickly test this approach today. Commented Feb 8, 2017 at 16:19
  • Hi, thanks for your replies. I think I will solve it with some kind of documentation like Sinatr mentioned. I think about a file lets call it DefaultApp.xaml and I put all my Default DynamicResources in this. And I also publish it to the Buildpath so everyone can consume this file and see which Resources he/she needs. Commented Feb 9, 2017 at 9:52

2 Answers 2

1

After InitializeComponent you can see if the resource you need is found and add a default if not.

 public MainWindow() { InitializeComponent(); try { var resource = FindResource("ForegroundColor"); } catch (ResourceReferenceKeyNotFoundException) { Resources.Add("ForegroundColor", new SolidColorBrush(Colors.Red)); } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, great idea but what is if anyone else uses my library and do not know anything about this resources. Maybe I can use your approach in the custom library itself. Let me try this :)
I don't see why you couldn't if you pass in the FrameworkElement as a parameter to a method. public void LoadResourceDefaults(FrameworkElement frameworkElement)
0

you are on a complex problem

see that

https://wangmo.wordpress.com/2007/09/27/themesgenericxaml/

Why is my style in Generic.xaml not working?

take care how you define your controldefault style

the place where you put themes\generic.xaml

where did you include your color scheme (normaly in generic)

check assemblyinfo

etc

if all is well structured, it must take your default color in the custom control assembly

or simply just include your generic from assembly in the app.xaml

<ResourceDictionary Source="pack://application:,,,/Client.Core;Component/themes/generic.xaml"/> 

1 Comment

you can also check some well known component library like wpftoolkit.codeplex.com - but certainly not by code like the previous answer...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.