Momo
Momo

Reputation: 123

Resize elements and split layout at runtime

I have a simple problem in WPF/C#. I want to drag and drop some images into a layout and adjust their sizes according to the number of images (it can be more than 2!).

What I want

I have no problem with the Drag&Drop system. My problem is about the resizing elements/splitting the layout. I tried several things such as using ItemsControl with a WrapPanel but it is not working. I just see the image 1 in full size.

Here my XAML

<ItemsControl x:Name="list" AllowDrop="True" 
    Drop="list_Drop" Background="Transparent" Grid.IsSharedSizeScope="True" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" BorderBrush="{x:Null}">
    <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
</ItemsControl>

And here my code-behind

private void list_Drop(object sender, DragEventArgs e)
{
    Thumbnail thumbnail = sender as Thumbnail;
    Image addedImg = new Image();
    addedImg.Source = (BitmapImage)e.Data.GetData(typeof(BitmapImage));
    list.Items.Add(addedImg);
    this.UpdateLayout();
}

Thank you !

Upvotes: 2

Views: 354

Answers (1)

Rhyous
Rhyous

Reputation: 6690

ItemsControl with the ItemsPanel set to UniformGrid.

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

Upvotes: 2

Related Questions