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?

有帮助吗?

解决方案

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?

其他提示

    <ListBox x:Name="name" ItemsSource="{Binding source}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <textbox x:Name="name"></ToggleButton> //or any tool
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top