1

My application has some areas which get colorized by the color codes I get from a web application. I thought a lot about how to achieve this and I don't know how to approach this problem.

My first thought was to write a ValueConverter which just provides those colors so that I can debug whenever it did work.

But now that I'm trying to use it on some styles I ran into the problem that I can't use ValueConverter in App.xaml.

So what I'm trying to archive is that when I navigate to something like the mainpage.xaml where no user specific content is shown I want to use the my company's colors but when the user navigates to pages where the content is user specific I want to display the company colors of the current user.

So I searched on stackoverflow how to accomplish this and I come across this post here. But I always get an binding failed message in the output window.

<Setter Property="Background"> <Setter.Value> <Binding Path="Background" RelativeSource="{RelativeSource Self}" TargetNullValue="a" FallbackValue="a"> <Binding.Converter> <provider:DarkBackgroundProvider/> </Binding.Converter> </Binding> </Setter.Value> </Setter> 

Any ideas or maybe an other approach on this?

I also read on some sites that I can replace values from App.xaml, but it did not work for me cause I got exceptions whenever I wanted to set the value of the App.Resources[] = Color

1 Answer 1

1

I reviewed your binding data error and see it below:

Exception thrown: 'System.FormatException' in PresentationCore.dll System.Windows.Data Error: 12 : TargetNullValue 'a' (type 'String') cannot be converted for use in 'Background' (type 'Brush'). BindingExpression:Path=Background; DataItem=null; target element is 'Button' (Name=''); target property is 'Background' (type 'Brush') FormatException:'System.FormatException: Token is not valid. 

The binding data error is not the issue of DarkBackgroundProvider but the issue of the TargetNullValue and FallbackValue.

You should create valid value for the two properties as below:

<Setter Property="Background"> <Setter.Value> <Binding Path="Background" RelativeSource="{RelativeSource Self}"> <Binding.TargetNullValue> <SolidColorBrush Color="White" /> </Binding.TargetNullValue> <Binding.FallbackValue> <SolidColorBrush Color="White" /> </Binding.FallbackValue> <Binding.Converter> <local:DarkBackgroundProvider /> </Binding.Converter> </Binding> </Setter.Value> </Setter> 

By changing your code into the one I've posted above the binding data error has disappeared.


Update

It's better to resolve your original issue instead of just resolve your binding error. Your original issue is to change a button style background due to the color you get from your web application.

So I've written the new code below to achieve your goal and it contains no converters.

<Application x:Class="Walterlv.Styles.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Walterlv.Styles" StartupUri="MainWindow.xaml"> <Application.Resources> <SolidColorBrush x:Key="Brush.MainButton.Background" Color="White" /> <Style TargetType="Button"> <Setter Property="Background" Value="{DynamicResource Brush.MainButton.Background}" /> </Style> </Application.Resources> </Application> 

This is the code-behind of the App.xaml. If you can get the instance of the App class you can change the color due to your newly calculated value.

public partial class App : Application { protected override async void OnStartup(StartupEventArgs e) { base.OnStartup(e); await Task.Delay(2000); Resources["Brush.MainButton.Background"] = new SolidColorBrush(Color.FromRgb(0x00, 0x7a, 0xcc)); } } 
Sign up to request clarification or add additional context in comments.

8 Comments

This did not work for me it appears in white not in the color from the provider
@MelloPs Of cause it does not work for you because you can never bind a property to the property itself and it definitely fails, that's the real reason why you fail. But you've asked another question that you get a binding error and you want to resolve it. I'll update my answer to resolve your original issue.
@MelloPs It's recommended to edit your question to post your original needs that you want to set the background of Button style from your provider.
Okay i did not know that this would make a difference since I thought the value converter would get the value when style gets applied so I thought that I only need this I will try this
Can I change the value at runtime? Because the specific problem ist that every user has its own color customization and user can be from more than on webapplication so the color needs to be changed when the user logs in
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.