Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Fit in Viewbox
Message
From
13/04/2009 16:42:30
 
 
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Title:
Environment versions
Environment:
C# 3.0
Miscellaneous
Thread ID:
01394627
Message ID:
01394676
Views:
43
>Hi,
>
>Simplest way to explain my requirements will be a simple example. So:
<Window x:Class="WpfApplication1.Window23"
>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
>    <StackPanel Orientation="Vertical">
>    <Canvas x:Name="Outer" Height="500" Width="500" Background="AliceBlue">
>        <Canvas x:Name="Main" Height="200" Width="200" Background="Red" Canvas.Top="200" ClipToBounds="True" >
>                <Canvas.RenderTransform>
>                    <RotateTransform Angle="345" />
>                </Canvas.RenderTransform>
>                    <Canvas x:Name="Contained" Height="300" Width="300" Background="Yellow" Canvas.Top="50" Canvas.Left="50">
>                <Canvas.RenderTransform>
>                    <RotateTransform Angle="345"/>
>                </Canvas.RenderTransform>
>            </Canvas>
>        </Canvas>
>    </Canvas>
>        <Border BorderBrush="Black" BorderThickness="2" Width="96" Height="96">
>        <Viewbox x:Name="VBox"  >
>        </Viewbox>
>            </Border>
>        <Button Click="Button_Click">Thumbnail it</Button>
>  </StackPanel>
></Window>
Code:
using System.Windows;
>using System.Windows.Controls;
>using System.Windows.Media;
>
>namespace WpfApplication1
>{
>  
>    public partial class Window23 : Window
>    {
>        public Window23()
>        {
>            InitializeComponent();
>        }
>
>        private void Button_Click(object sender, RoutedEventArgs e)
>        {
>            Outer.Children.Remove(Main);
>            Main.LayoutTransform = Main.RenderTransform;
>            Main.RenderTransform = Transform.Identity;
>            VBox.Child = Main;
>        }
>    }
>}
This does what I want. However if ClipToBounds is not set on Main then the sizing is obviously wrong. Whats the simplest way of getting this right?
>
>I originally did this without using a ViewBox by using GetDescendantBounds, adding a ScaleTransform etc. It worked OK but got pretty ugly - basing the operation around a ViewBox should be much simpler.
>
>Any suggestions,
>Viv

I played with this for a bit and so far, no matter what I try, I cannot get GetDescendantBounds to calculate a correct width. I'm close, within a few pixels, but I am still off. Frustrating!

Heres what I got, I think this is close to what you wanted, but it is still off!
<Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight">
  <StackPanel Orientation="Vertical">
    <Canvas x:Name="Outer" Height="500" Width="500" Background="AliceBlue">
      <Canvas x:Name="cvsMeasure" Height="0" Width="0" Canvas.Top="200" >
        <Canvas x:Name="Main" Height="200" Width="200" Background="Red" >
        <Canvas.RenderTransform>
          <RotateTransform Angle="345" />
        </Canvas.RenderTransform>
        <Canvas x:Name="Contained" Height="300" Width="300" Background="Yellow" Canvas.Top="50" Canvas.Left="50" >
          <Canvas.RenderTransform>
            <RotateTransform Angle="345"/>
          </Canvas.RenderTransform>
        </Canvas>
      </Canvas>
      </Canvas>
    </Canvas>
    <Border BorderBrush="Black" BorderThickness="2" Width="96" Height="96"> 
      <Viewbox x:Name="VBox" >
        <Canvas x:Name="VBoxCanvas" Background="AliceBlue" ></Canvas>
      </Viewbox>
    </Border>
    <Button Click="Button_Click">Thumbnail it</Button>
  </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication2
  {
  /// <summary>
  /// Interaction logic for Window1.xaml
  /// </summary>
  public partial class Window1 : Window
    {
    public Window1()
      {
      InitializeComponent();
      }

    private void Button_Click(object sender, RoutedEventArgs e)
      {
      //Main.LayoutTransform = Main.RenderTransform;
      //Main.RenderTransform = Transform.Identity;
      Rect bounds = VisualTreeHelper.GetDescendantBounds(cvsMeasure);
      Double size = Math.Max(bounds.Size.Width + (bounds.Left*2) , bounds.Size.Height + bounds.Top);
      cvsMeasure.Children.Remove(Main);
      Main.SetValue(Canvas.TopProperty, 0-bounds.Top );
      Main.SetValue(Canvas.LeftProperty, 0 - bounds.Left);
      VBoxCanvas.Width = size;
      VBoxCanvas.Height = size;
      VBoxCanvas.Children.Add(Main);
      //VBox.Child = Main;
      }

    }
  }
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform