Search code examples
xamlwindows-runtimetextblock

The member "Background" is not recognised or is not accessible


I am trying to define a style for a TextBlock. Found this example on MSDN of how to set a style (it is in the first code block).

My XAML is:

<Page
    x:Name="pageRoot"
    x:Class="MyPrototype.MainPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MyPrototype"
    xmlns:common="using:MyPrototype.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

<Page.Resources>
    <!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
    <x:String x:Key="AppName">MyPrototype</x:String>
    <x:String x:Key="AppTitle">MyPrototype</x:String>

    
    <Style TargetType="TextBlock" x:Key="TitleTextBlockStyle">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="FontFamily" Value="Trebuchet MS"/>
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontSize" Value="18"/>
    </Style>
</Page.Resources>

<StackPanel Orientation="Horizontal" >
<TextBlock Style="{StaticResource TitleTextBlockStyle}" Text="My Prototype Main Page"/>
</StackPanel>

I am getting the error:

The member "Background" is not recognised or is not accessible

and I can't see why. I guess a syntax error, but can't see what it is.


Solution

  • This is the documentation that applies to your type of application.

    In this case TextBlock has no Background property.