Entwickler-Ecke

WPF / Silverlight - Type-Key für Style wird nicht gefunden


Kasko - Mo 26.07.21 03:54
Titel: Type-Key für Style wird nicht gefunden
Ich habe eine einfache Klasse FlatButton gesprieben, welche von Button erbt:

XAML:

XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
<Button
    x:Class="Program.GUI.Controls.FlatButton"
    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:GUIControls="clr-namespace:Program.GUI.Controls"
    mc:Ignorable="d">
    <Button.Resources>
        <ResourceDictionary>
            <Style x:Key="{x:Type GUIControls:FlatButton}" TargetType="{x:Type GUIControls:FlatButton}" BasedOn="{StaticResource ResourceKey={x:Type Button}}">
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GUIControls:FlatButton}">
                            <Grid x:Name="LayoutRoot" Background="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}">
                                <TextBlock
                                    x:Name="Text"
                                    Text="{TemplateBinding Content}"
                                    Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}"
                                    HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                    VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="LayoutRoot" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverBackground}"/>
                                    <Setter TargetName="Text" Property="Foreground" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=HoverForeground}"/>
                                </Trigger>
                                <Trigger Property="IsMouseCaptured" Value="True">
                                    <Setter TargetName="LayoutRoot" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ClickBackground}"/>
                                    <Setter TargetName="Text" Property="Foreground" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ClickForeground}"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Button.Resources>
</Button>


CS:

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Program.GUI.Controls {
    /// <summary>
    /// Interaction logic for FlatButton.xaml
    /// </summary>
    public partial class FlatButton : Button {
        public static readonly DependencyProperty HoverBackgroundProperty = DependencyProperty.Register(
            "HoverBackground",
            typeof(Brush),
            typeof(FlatButton)
        );

        public static readonly DependencyProperty HoverForegroundProperty = DependencyProperty.Register(
            "HoverForeground",
            typeof(Brush),
            typeof(FlatButton)
        );

        public static readonly DependencyProperty ClickBackgroundProperty = DependencyProperty.Register(
            "ClickBackground",
            typeof(Brush),
            typeof(FlatButton)
        );

        public static readonly DependencyProperty ClickForegroundProperty = DependencyProperty.Register(
            "ClickForeground",
            typeof(Brush),
            typeof(FlatButton)
        );

        public Brush HoverBackground { get; set; }
        public Brush HoverForeground { get; set; }

        public Brush ClickBackground { get; set; }
        public Brush ClickForeground { get; set; }

        static FlatButton() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatButton), new FrameworkPropertyMetadata(typeof(FlatButton)));
        }

        public FlatButton() {
            this.InitializeComponent();
        }
    }
}


Wenn ich jetzt aber versuche, den Style des Buttons in der MainWindow-XAML zu erweitern, wird der Type-Key nicht gefunden:


XML-Daten
1:
2:
3:
4:
5:
6:
7:
8:
<Style x:Key="DefaultDarkButtonThemeStyle" TargetType="{x:Type GUIControls:FlatButton}" BasedOn="{StaticResource {x:Type GUIControls:FlatButton}}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="{DynamicResource ColorTextPrimaryDark}"/>
    <Setter Property="HoverBackground" Value="{DynamicResource ColorPrimaryDarkLight}"/>
    <Setter Property="HoverForeground" Value="{DynamicResource ColorTextPrimaryDarkLight}"/>
    <Setter Property="ClickBackground" Value="{DynamicResource ColorPrimaryDarkLightLight}"/>
    <Setter Property="ClickForeground" Value="{DynamicResource ColorTextPrimaryDarkLightLight}"/>
</Style>


Exception: (Orginal Wortlaut)


Quelltext
1:
2:
3:
4:
System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number '45' and line position '47'.'

Inner Exception
Exception: Cannot find resource named 'Program.GUI.Controls.FlatButton'. Resource names are case sensitive.


Th69 - Mo 26.07.21 08:42

Worauf bezieht sich denn genau

Quelltext
1:
Line number '45' and line position '47'                    
in der "MainWindow.xaml"?

Es könnte sein, daß die lokalen Button.Resources nicht von außerhalb zugreifbar sind, und du diese als globale Ressource zur Verfügung stellen mußt, s.a. WPF Tutorial: Resources [https://wpf-tutorial.com/wpf-application/resources/] (unter "Local and application wide resources").