CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2018
    Posts
    4

    [RESOLVED] WPF XAML: Declaring App-wide colors and brushes and accessing them

    What is it I don't understand about declaring or accessing XAML resources? I'm expecting the foreground text in the TextBlock (which is on the Canvas in the MainWindow) to be the color defined in the App-wide Style, but it is White (testing implies the Foreground color's default is the Background color). Intellisense even gives me the choice of the values I have used. I have tried changing "{StaticResrouce"s to "{DynamicResource"s to no effect.

    App.xaml:
    Code:
    <Application x:Class="TestApp.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:TestApp"
                 xmlns:System="clr-namespace:System;assembly=mscorlib"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <!-- Declare a key to hold a variant of Orange (easy to see on a white or black background). -->
            <Color x:Key="AppForegroundColor" R="255" G="144" B="0" />
            <SolidColorBrush x:Key="AppBrush" Color="{StaticResource AppForegroundColor}" />
            <System:Double x:Key="AppFontSize">14.0</System:Double>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="{StaticResource AppBrush}"/>
                <Setter Property="HorizontalAlignment" Value="Left"/>
                <Setter Property="Height" Value="16"/>
                <Setter Property="VerticalAlignment" Value="Top"/>
                <Setter Property="Width" Value="512"/>
                <Setter Property="FontFamily" Value="FreeSans"/>
                <Setter Property="FontSize" Value="{StaticResource AppFontSize}"/>
            </Style>
        </Application.Resources>
    </Application>
    MainWindow.xaml:
    Code:
    <Window x:Class="TestApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:TestApp"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Canvas>
            <TextBlock Canvas.Left="20" Canvas.Top="20" Height="40" Width="200" Text="This is test text."/>
                     <!-- The text above draws as White. -->
                     <!-- If I add the Foreground property with a value of "{StaticResource AppBrush}" the text still draws as White. -->
                     <!-- If I add the Foreground property with a value of "Blue" the text draws as Blue. -->
        </Canvas>
    </Window>
    Last edited by GregJ7; March 17th, 2018 at 07:14 AM.

  2. #2
    Join Date
    Jan 2018
    Posts
    4

    Re: WPF XAML: Declaring App-wide colors and brushes and accessing them

    Problem was not having an A= value in the <Color tag.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured