开发者

Dynamically adding controls to View from Controller

开发者 https://www.devze.com 2023-01-29 18:50 出处:网络
In WPF, I am using the MVVM mo开发者_如何学JAVAdel. 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

In WPF, I am using the MVVM mo开发者_如何学JAVAdel.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消