简体   繁体   English

绑定到App.xaml中的属性

[英]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. 我有一个快速的问题:我想绑定一个Solidcolorbrush,它位于App.xaml的资源中。 It has a key assigned to it, but how would I bind to that property from another page? 它具有分配给它的密钥,但是如何从另一个页面绑定到该属性?

Here is the App.xaml itself: 这是App.xaml本身:

<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? 如果然后有一个要绑定到PrimaryAccentColorBrush的页面,该绑定将如何显示?

I tried setting the bind to {Binding Path={StaticResource PrimaryAccentColorBrush}} , but it did not locate it. 我尝试将绑定设置为{Binding Path={StaticResource PrimaryAccentColorBrush}} ,但是找不到它。

Any help would be appreciated =) 任何帮助将不胜感激=)

您不必使用Binding ,只需使用StaticResource ,就像您已经做的那样:

Property="{StaticResource PrimaryAccentBrush}"

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 ? 尽管您的示例中似乎没有PrimaryAccentColorBrush ...您是说PrimaryAccentBrush吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM