Bacon
Bacon

Reputation: 823

Styling A Custom WPF Control Not Working

I created a custom WPF control and styling doesn't seem to work on it. I've tried creating external styles and internal styles and neither seem to work. Below is the code for the internal style.

Custom Control

public class NavigationTextBlock : TextBlock
{
    static NavigationTextBlock()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(NavigationTextBlock),
            new FrameworkPropertyMetadata(typeof(NavigationTextBlock)));
    }

    public static readonly DependencyProperty IsSelectedProperty =
        DependencyProperty.Register("IsSelected", typeof(bool), typeof(NavigationTextBlock), new UIPropertyMetadata(false));

    public bool IsSelected
    {
        get
        {
            return (bool)GetValue(IsSelectedProperty);
        }
        set
        {
            SetValue(IsSelectedProperty, value);
        }
    }
}

XAML

None of the styling under <c:NavigationTextBlock.Styles> are applied.

<UserControl x:Class="Fallout4Checklist.Views.NavigationView"
         xmlns:metro="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:Fallout4Checklist.Views"
         xmlns:c="clr-namespace:Fallout4Checklist.Controls"
         xmlns:cal="http://www.caliburnproject.org"
         mc:Ignorable="d"
         d:DesignHeight="100" 
         d:DesignWidth="800">
<Border Padding="16, 0, 16, 0">
    <ItemsControl ItemsSource="{Binding NavigationItems}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="1" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <c:NavigationTextBlock 
                FontFamily="Segoe UI"
                FontSize="24"
                FontWeight="SemiBold"
                Foreground="DarkGray" 
                VerticalAlignment="Center"
                TextAlignment="{Binding TextAlignment}" 
                Text="{Binding Content}">
                    <c:NavigationTextBlock.Style>
                        <Style TargetType="{x:Type c:NavigationTextBlock}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsMouseOver}" Value="True">
                                    <Setter Property="Foreground" Value="#CC119EDA" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding IsSelected}" Value="False">
                                    <Setter Property="Foreground" Value="DarkGray" />
                                </DataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected}" Value="True" />
                                        <Condition Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Foreground" Value="#0d78a6" />
                                </MultiDataTrigger>
                                <MultiDataTrigger>
                                    <MultiDataTrigger.Conditions>
                                        <Condition Binding="{Binding IsSelected}" Value="False" />
                                        <Condition Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True" />
                                    </MultiDataTrigger.Conditions>
                                    <Setter Property="Foreground" Value="#8c8c8c" />
                                </MultiDataTrigger>
                            </Style.Triggers>
                        </Style>
                    </c:NavigationTextBlock.Style>
                </c:NavigationTextBlock>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</Border>
</UserControl>

Upvotes: 0

Views: 269

Answers (1)

Slime recipe
Slime recipe

Reputation: 2263

You are setting the properties directly to the control, that will override the style.

Remove

Foreground="DarkGray" 

Upvotes: 1

Related Questions