Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to fill a WrapPanel dynamically with Images,
The Window that contains the WrapPanel is resizable, and i would like to
make the images within the wrappanel resize when they got more space.



My problem is that i want the images to resize if i add more to the wrappanel.

For example:
If there is only ONE image, it should take all the space it can,
but if i add another, they should both be resized, so that both only take half the space.
Both images should grow if i resize the Window and make it bigger, but they should also shrink if i shrink the Window.

I'm sorry for my terrible english-
I hope i could explain my problem.

My Code:
C#
string path = s;
                   BitmapImage bit = new BitmapImage();
                   Uri ur = new Uri(path);
                   bit.BeginInit();
                   bit.UriSource = ur;
                   bit.EndInit();

                   System.Windows.Controls.Image tempimage = new System.Windows.Controls.Image();
                   tempimage.Source = bit;


                  tempimage.Stretch = Stretch.Uniform;

                  //### THESE DID NOT WORK
                  // tempimage.Height = Double.NaN;
                  //tempimage.Width = Double.NaN;
                   //wrapPanel1.ItemHeight = Double.NaN;
                   //wrapPanel1.ItemWidth = Double.NaN;
                   //###


                   wrapPanel1.Children.Add(tempimage);



Can anybody help me?
Posted

1 solution

Hi Feverbird,

Just use UniformGrid instead, for example:

MainWindow.cs:

XML
<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel LastChildFill="True">
        <Border DockPanel.Dock="Top">
            <Button Width="100" Content="Add" Click="Button_Click" />
        </Border>
        <UniformGrid x:Name="panel"  />
    </DockPanel>
</Window>


Code Behind:

C#
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var image = new BitmapImage(new Uri("Choice_toxicity_icon.png", UriKind.RelativeOrAbsolute));

        panel.Children.Add(new Image() { Source = image , Stretch = Stretch.Fill});
    } 
}


Best Regards,

Shai
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900