Frage

I want to create a ListBox and fill it with anonymous UIElement. In other words, the DataTemplate of the ItemTemplate will contain only one element, and afterwords during the runtime, afterwards I will create in the code behind different UIElements (TextBlocks, Grids ...) and populate the list with it.

So how am I going to write the DataTemplate of the ListBox? And how am I intend to use an ObservableCollection for the ItemSource? So should use an ObsevableCollection of UIElement?

War es hilfreich?

Lösung

First Question: No DataTemplate. Since your items are already UIElements, you don't need a DataTemplate.

And if you're creating a changeable collection, then yes, ObservableCollection<UIElement> is the way to go.

Now, why would you be doing this? You may want to ask yourself if this is the best way of doing things. Why isn't your data and your presentation separated? If you need more than one type of element in the list, will DataTemplateSelector allow you to have a real ViewModel?

Andere Tipps

    <ListBox x:Name="name" ItemsSource="{Binding source}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <textbox x:Name="name"></ToggleButton> //or any tool
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top