Tokfrans
Tokfrans

Reputation: 1959

Bind to property in App.xaml

I have a quick question: I want to bind a Solidcolorbrush, which is located in App.xaml's Resources. It has a key assigned to it, but how would I bind to that property from another page?

Here is the App.xaml itself:

<Application x:Class="Mplayer.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <Color x:Key="PrimaryAccentColor" A="255" R="3" G="169" B="244"/>
        <SolidColorBrush x:Key="PrimaryAccentBrush" Color="{StaticResource PrimaryAccentColor}"/>
        <Color x:Key="SecondaryAccentColor" A="255" R="3" G="169" B="244"/>
        <SolidColorBrush x:Key="SecondaryAccentBrush" Color="{StaticResource SecondaryAccentColor}"/>
        <Color x:Key="LightBackgroundColor" A="255" R="3" G="169" B="244"/>
        <SolidColorBrush x:Key="LightBackgroundBrush" Color="{StaticResource LightBackgroundColor}"/>
        <Color x:Key="DarkBackgroundColor" A="255" R="3" G="169" B="244"/>
        <SolidColorBrush x:Key="DarkBackgroundBrush" Color="{StaticResource DarkBackgroundColor}"/>
    </ResourceDictionary>
</Application.Resources>

If I then have a page which I want to bind to the PrimaryAccentColorBrush, how would that bind look?

I tried setting the bind to {Binding Path={StaticResource PrimaryAccentColorBrush}}, but it did not locate it.

Any help would be appreciated =)

Upvotes: 0

Views: 299

Answers (2)

Patrick Hofman
Patrick Hofman

Reputation: 157098

You don't have to use Binding, just use StaticResource, like you already do:

Property="{StaticResource PrimaryAccentBrush}"

Upvotes: 3

Sheridan
Sheridan

Reputation: 69987

You almost had it... try this instead:

<TextBlock Background="{Binding Source={StaticResource PrimaryAccentColorBrush}}" />

Although you don't seem to have a PrimaryAccentColorBrush in your example... did you mean PrimaryAccentBrush?

Upvotes: 1

Related Questions