Reputation: 135
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
Upvotes: 1
Views: 328
Reputation: 2376
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.
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));
}
}
Upvotes: 1