WPF/XAML: Menge von "best" Controls anlegen



  • Hallo Leute,

    ist schwer zu beschreiben, deswegen ein kurzer pseudo code:

    <MyFooControl>
    <MyFooControl.Modules>
    < MyBarControl>
    < MyBarControl>
    < MyBarControl>
    < MyBarControl>
    </MyFooControl.Modules>
    </MyFooControl>
    

    Ich habe also ein Control , welches eine Eigenschaft haben soll, in der ich eine Menge an "MyBarcontrol" anfügen kann! Zudem soll geprüft werden , ob die Control auch vom Typ <MyBarControl> sind!

    Ähnlich wie beim Combobox und CombboxItem! Allerding weiß ich wegen nich wie der Code dahinter aussierht, damit ich abschaun kann:)

    Danke schonmal 🙂



  • Du meinst, so etwas wie ItemsControl (diese ist Basisklasse der Combo- und ListBox)?

    Du könntest dein Control davon ableiten und dann die passenden virtuellen Methoden überschreiben.

    Wenn's auch gleich mit Auswahl (Selection) sein soll, dann Selector als Basisklasse nehmen.



  • Hallo,

    Hab die optimale Lösung:)

    Mit dem Contemporary Attributes und dem entsprechenden itemsourceproperty kownnen mehrere Elemente also content gesetzt werden

    [ContentProperty("Items")]
    public partial class MyUserControl2 : UserControl
    {
        public static readonly DependencyProperty ItemsSourceProperty = 
            ItemsControl.ItemsSourceProperty.AddOwner(typeof(MyUserControl2));
        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }
    
        public ItemCollection Items
        {
            get { return _itemsControl.Items; }
        }
    
        public MyUserControl2()
        {
            InitializeComponent();
        }
    }
    


  • Xamel dazu

    <UserControl x:Class="Test.UserControls.MyUserControl2"
                 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"
                 Name="control">
        <Grid>
            <Button>Just a button</Button>
            <ItemsControl Name="_itemsControl" ItemsSource="{Binding ItemsSource, ElementName=control}"/>
        </Grid>
    </UserControl>
    

Anmelden zum Antworten