LB.
LB.

Reputation: 14112

Dynamically adding controls to View from Controller

In WPF, I am using the MVVM model.

I have a View with a UniformGrid and a ViewModel where I would like to add items to the UniformGrid how would I accomplish this without doing it in the code behind?

Upvotes: 0

Views: 401

Answers (1)

Aaron McIver
Aaron McIver

Reputation: 24713

The UniformGrid is a Panel; it's intent is not what you are trying to accomplish. You can achieve what you are trying to do however by adjusting the default ItemsPanelTemplate within an ItemsControl.

        <ItemsControl ItemsSource="{Binding PropertyNameOnViewModel}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

This will allow you to bind to your ViewModel property within the ItemsControl and the data will get represented visually within a UniformGrid.

Upvotes: 2

Related Questions