Search code examples
c#xamlresourcedictionarystaticresource

xaml resourcedictionary content setter foreground not displaying correctly


I am attempting to make a ResourceDictionary of some standardised colour schemes etc to add to a dll class library for use in future applications. I am new to XAML and seem to have made an error when it comes to creating or using the content setter part of the dictionary. I cannot set and use the colour of the text in the dictionary. Here is what I have so far; As you can see, the Foreground of the TextBlock in the Content is set to White.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:My_Class_Library.WPF_Resources">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../WPF Resources/Buttons.xaml"/>
        <ResourceDictionary Source="../WPF Resources/Brushes.xaml"/>
        <ResourceDictionary Source="../WPF Resources/Sliders.xaml"/>
    </ResourceDictionary.MergedDictionaries>
    <Style x:Key="myButton" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button" >
                    <Grid>
                        <Rectangle Name="ClickFill" Fill="ForestGreen" RadiusX="5" RadiusY="5"/>
                        <ContentPresenter   RecognizesAccessKey="True" Content="{TemplateBinding Content}"
                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Content">
            <Setter.Value>
                <Grid>
                    <TextBlock Background="{x:Null}" Foreground="White"></TextBlock>
                </Grid>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

and here's the reference to the dictionary:

<Grid x:Class="My_Class_Library.Update_Information"
        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:My_Class_Library"
        HorizontalAlignment="Center" VerticalAlignment="Center">
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../WPF Resources/Buttons.xaml"/>
                <ResourceDictionary Source="../WPF Resources/Brushes.xaml"/>
                <ResourceDictionary Source="../WPF Resources/Sliders.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    <Button Name="Click"
            Width="100" Height="30"
            Style="{StaticResource myButton}"
            Click="Click_Click">
        _click
    </Button>
</Grid>

however, what I see is this: black text, not white, which as you can see has black (presumably default) text instead of the specified white. What am I doing wrong, that causes the content not to be set?

I know everybody hates questions like "look at this, what's wrong with it?" but I am at my wit's end trying to find a solution - I am following loads of training videos and so on and the above is my best effort... Nearly everything else I try breaks the whole thing! Any pointers very appreciated!


Solution

  • You've kind of got the right idea but you'll want to get used to setting properties from the style template as setter directly. I'd also suggest you start with a default style template of a control and edit it to your needs since like for instance in this case you're going to lose all of the additional Visual states for things like MouseOver etc.

    However for the sake of your immediate question, you're going to ditch your Content property all together from the template (that's your ContentPresenter job) and instead just do this (in pseudo);

    <Style x:Key="myButton" TargetType="Button">
       <Setter Property="Foreground" Value="White"/>
       <!-- Rest of it goes here -->
    </Style>
    

    ...and voila. Hope this helps, cheers!