0

I am dynamically adding images using view boxes to a UniformGrid.

for (int i = 0; i < count; i++)
{
    var viewbox = new Viewbox();
    var filePath = "myFilePath";

    if (!File.Exists(filePath)) continue;

    var newImage = new Image();
    var bitmapImage = new BitmapImage(new Uri(filePath));
    newImage.Source = bitmapImage;

    viewbox.Child = newImage;
    viewbox.SetValue(Grid.RowProperty, i);

    ImageGrid.Children.Add(viewbox);
}

The problem I'm running into is where the images are different sizes, horizontally or vertically.

A good way to visualize whats happening. If there's 2 images on screen, with the first image being wider and shorter than the second image. When I shrink the window horizontally, the first image will shrink independently its the same width as the second. Now, when I shrink the window vertically, the second image will shrink independently until its the same height as the first.

How do I make the 2 images scale together without setting the view box stretch property to fill?

Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Chutes
  • 123
  • 1
  • 8
  • 1 - You shouldn't be creating/manipulating UI elements in code, that's no the right way to do it in WPF. 2 - post a screenshot of what you need and what you have. If you can't here in SO then try `imgur.com`. – Federico Berasategui Apr 09 '13 at 04:08
  • My UI is dynamically created using my data layer. The only xaml i have is the Tab Control. Tab Items and everything else is dynamically created. The UI has no knowledge of the data before run time. It doesnt know how many tab items are going to be needed or even how many images need to be displayed. The UI only knows how the data will be structured and builds the UI elments at runtime base on whatever its given – Chutes Apr 09 '13 at 05:15
  • that's irrelevant. You should be using MVVM to dynamically create the UI via `ItemsControl`s instead. WPF is not winforms. Actually, a `TabControl` is an `ItemsControl`, therefore you should bind that to some `IEnumerable` in the ViewModel. That way you can define your views in pure XAML and not resorting to a whole bunch of horrible hacks. – Federico Berasategui Apr 09 '13 at 13:06
  • I dont know if they're horrible hacks or really just a different style... Kind of goes along with the argument of putting code in the code behind. However, I was thinking about my current design more last night and you're definitely right. I could have created the views in pure xaml, binding the item source of the tab control to a some IEnumerable of T. – Chutes Apr 09 '13 at 15:16
  • either that, or create some XAML-defined `Styles` and then when creating the UI elements in code, assign them these styles. That way you alleviate a lot of nasty property-setting code and put it in reusable styles. Anyways I'm against of creating UI elements in code. It creates a maintainability chaos. – Federico Berasategui Apr 09 '13 at 15:19
  • Regardless, even if I had, the behavior of the controls wouldn't be any different. Viewboxes are great for making images fill a space while holding their aspect ratio even while manually resizing the window. What I'm looking for though is something like a view box list where the viewboxes change size together rather than separately – Chutes Apr 09 '13 at 15:22
  • Oh definitely. Styles are much easier to write in xaml than code. Most of my 'Styles' are already in the app resources. – Chutes Apr 09 '13 at 15:24

0 Answers0