jak utworzyć drzewko DependencyProperties?

0

Witam wszystkich serdecznie!
Mam Kontrolkę użytkownika, a w niej 3 inne (te same) kontrolki użytkownika. Chcę stworzyć grupę DependencyProperty. Stworzyłem klasę dziedziczącą po DependencyObject:

public class ImageButtonDependencyProperty : DependencyObject
    {
        public ImageButtonDependencyProperty(eImage ImageType, string Text)
        {
            this.ImageType = ImageType;
            this.Text = Text;
        }

        public eImage ImageType
        {
            get { return (eImage)GetValue(ImageTypeProperty); }
            set { SetValue(ImageTypeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ImageType.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ImageTypeProperty =
            DependencyProperty.Register("ImageType", typeof(eImage), typeof(ImageButtonDependencyProperty), new PropertyMetadata(eImage.Nothing));
        
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(ImageButtonDependencyProperty), new PropertyMetadata(""));

        public Brush Foreground
        {
            get { return (Brush)GetValue(ForegroundProperty); }
            set { SetValue(ForegroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Foreground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ForegroundProperty =
            DependencyProperty.Register("Foreground", typeof(Brush), typeof(ImageButtonDependencyProperty), new PropertyMetadata(Brushes.Black));
    }

utworzyłem DependencyProperty wewnątrz kontrolki:

public ImageButtonDependencyProperty ButtonStyle
        {
            get { return (ImageButtonDependencyProperty)GetValue(ButtonStyleProperty); }
            set { SetValue(ButtonStyleProperty, value); }
        }
        public static readonly DependencyProperty ButtonStyleProperty =
            DependencyProperty.Register
            (
                "ButtonStyle", 
                typeof(ImageButtonDependencyProperty), 
                typeof(ImageButton), 
                new PropertyMetadata(new ImageButtonDependencyProperty(eImage.Nothing, "")
            ));

Nie mogę jednak bindować do DependencyProperty wewnątrz DependencyProperty:

[Wyszukiwanie Property]

screenshot-20180802003534.png

====================================
[Wybranie property]

screenshot-20180802003612.png

====================================
Chciałbym uzyskać efekt taki jak tu:

screenshot-20180802003725.png

====================================
macie jakieś porady dla nowicjusza? Staram się jak mogę naprawdę :)

0

O ile dobrze pamiętam to po kropce możesz mieć tylko "Attached property", zwykłych "dependency property" tak się nie da zrobić.

0

Zrobiłem także z AttachedProperty według poradnika, jednak za żadne skarby nie mogę odwołać się do DP wewnątrz klasy:
Wybaczcie mi naprawdę, że tak wolno odpisuję, ale zajebany robotą dziś byłem ;(

public class Test : DependencyObject
    {
        static Test()
        {

        }
        
        public static string Get_Text(DependencyObject obj)
        {
            return (string)obj.GetValue(_TextProperty);
        }

        public static void Set_Text(DependencyObject obj, string value)
        {
            obj.SetValue(_TextProperty, value);
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty _TextProperty =
            DependencyProperty.RegisterAttached("_Text", typeof(string), typeof(Test), new PropertyMetadata("Dlaczego to nie działa :("));
    }
public static void SetMyProp(DependencyObject obj, Test value)
        {
            obj.SetValue(MyPropProperty, value);
        }

        public static readonly DependencyProperty MyPropProperty =
            DependencyProperty.RegisterAttached("MyProp", typeof(Test), typeof(ImageButton), new PropertyMetadata());

Już jest jakiś postęp, ale dalej nie mogę odwołać się do Property _Text

screenshot-20180802215002.png

0

Chodzi mi o 2 instancje klas w kontrolce:
screenshot-20180802220303.png

a następnie w Kontrolce xaml:

<Button Content={Binding path="Button1Option.Text"}/>

Gdy tworzę kontrolkę ustawiam po prostu:

<UC:MyUserControl Button1Option.Text="Jakiś tam tekst"/>
2

Czy widziałeś gdzieś w WPF więcej niż jedną kropkę po lewej stronie ?

zero kropek -> dependency property, albo zwykłe property
jedna kropka -> attached property
więcej kropek -> nie możliwie

mówiąc wprost, nie zrobisz tego w ten sposób.

Zresztą to co chcesz zrobić łamie zarówno enkapsulacje, jak i prawo demeter z niej wynikające.

1 użytkowników online, w tym zalogowanych: 0, gości: 1