Laden...

Style Problem

Erstellt von enen vor 12 Jahren Letzter Beitrag vor 12 Jahren 1.202 Views
E
enen Themenstarter:in
16 Beiträge seit 2010
vor 12 Jahren
Style Problem

Hallo @All,

ich habe mal eine Frage zu einem Style. Ich wollte mal versuchen ein eigenes Control zur erstellen und dem Teil ein Template zu verpassen. Das Control ist folgendermaßen definiert.


namespace Controls
{   
    public enum HeaderPosition : ushort
    {
        LeftTop = 0,
        RightTop=1,

        LeftBottom=2,
        RightBottom=4,

        LeftCenter=8,
        RightCenter=16
    }

    [ContentProperty("Content")]
    public class LabeledPanel : Control
    {
        static LabeledPanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledPanel), new FrameworkPropertyMetadata(typeof(LabeledPanel)));
        }

        public object Content
        {
            get { return (object)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(LabeledPanel), new UIPropertyMetadata(null));
        
        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderProperty =
            DependencyProperty.Register("Header", typeof(object), typeof(LabeledPanel), new UIPropertyMetadata(null));

        public HeaderPosition HeaderPosition
        {
            get { return (HeaderPosition)GetValue(HeaderPositionProperty); }
            set { SetValue(HeaderPositionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderPosition.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderPositionProperty =
            DependencyProperty.Register("HeaderPosition", typeof(HeaderPosition), typeof(LabeledPanel), new UIPropertyMetadata(HeaderPosition.LeftTop));        

        public Thickness HeaderMargin
        {
            get { return (Thickness)GetValue(HeaderMarginProperty); }
            set { SetValue(HeaderMarginProperty, value); }
        }

        // Using a DependencyProperty as the backing store for HeaderMargin.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty HeaderMarginProperty =
            DependencyProperty.Register("HeaderMargin", typeof(Thickness), typeof(LabeledPanel), new UIPropertyMetadata(new Thickness(0,0,12,0)));

        public Thickness ContentMargin
        {
            get { return (Thickness)GetValue(ContentMarginProperty); }
            set { SetValue(ContentMarginProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContentMargin.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentMarginProperty =
            DependencyProperty.Register("ContentMargin", typeof(Thickness), typeof(LabeledPanel), new UIPropertyMetadata(new Thickness(0,0,0,0)));                
    }
}

Das zugefhörige Template sieht folgendermaßen aus.


<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Controls">
    <Style TargetType="{x:Type local:LabeledPanel}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:LabeledPanel}">
                    <DockPanel Background="{TemplateBinding Background}" LastChildFill="True" >
                        <TextBlock Text="{Binding Header}" Margin="{Binding HeaderMargin}">
                            <TextBlock.Style>
                                <Style TargetType="{x:Type local:LabeledPanel}">
                                    <Style.Triggers>
                                        <Trigger Property="HeaderPosition}" Value="LeftTop" >
                                            <Setter Property="DockPanel.Dock" Value="Top" />
                                            <Setter Property="TextBlock.HorizontalAlignment" Value="Left" />
                                        </Trigger>                                        
                                    </Style.Triggers>
                                </Style>
                            </TextBlock.Style>
                        </TextBlock>
                        <ContentPresenter Content="{TemplateBinding Content}" Margin="{Binding HeaderMargin}" />
                    </DockPanel>                    
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Beim Kompilieren erhalte ich den Fehler, das die Style Property 'HeaderPosition' im Typen nicht vorhanden ist.

Wieso? Ich sehe die Lösung nicht.

Grüße

Enen

6.862 Beiträge seit 2003
vor 12 Jahren

Hallo,

Beim Kompilieren erhalte ich den Fehler, das die Style Property 'HeaderPosition' im Typen nicht vorhanden ist.

Nein, die erhälst du nicht. Du erhälst die Meldung, dass 'HeaderPosition**}**' nicht gefunden werden kann. Du musst genau lesen!

Machst du das Control ansonsten zur Übung oder so? Weil du machst nichts anderes als das HeaderedContentControl nachzuprogrammieren. Mit Panels hat dein Control nicht mal was zu tun, du nennst es zwar so, aber da es von Control statt von Panel abgeleitet ist, ists kein Panel 😃

Baka wa shinanakya naoranai.

Mein XING Profil.

E
enen Themenstarter:in
16 Beiträge seit 2010
vor 12 Jahren

Das ist in der Tat nur ein Übung. HeaderedContentControl ist sicher die bessere Wahl.

Aber warum wird die Property nicht gefunden?

S
24 Beiträge seit 2011
vor 12 Jahren
<Trigger Property="HeaderPosition}" Value="LeftTop" >
6.862 Beiträge seit 2003
vor 12 Jahren

Ich kann mich da nur selber zitieren...

Du musst genau lesen!

Baka wa shinanakya naoranai.

Mein XING Profil.

E
enen Themenstarter:in
16 Beiträge seit 2010
vor 12 Jahren

Ok,

manchmal sieht man den Wald vor lauter Bäumen nicht. Das zweite Zitat hat geholffen 😉

Danke

Enen