1

I have a WPF control that is roughly layed out like this:

<ViewBox Stretch="Uniform" Name="viewboxName">
    <ItemsControl>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <!-- a bunch of controls here that I want stretched in the viewbox -->
             </DataTemplate>
         </ItemsControl.ItemTemplate>
    </ItemsControl>
</ViewBox>

Then, in the AdornerLayer, I have (using a technique based on http://shevaspace.blogspot.com/2007/02/visual-level-programming-vs-logical.html) a button control defined as

<Button>
    <Image Source="/AcchImageLoad;component/icons/metroStudio/ImageRotation.png" Stretch="None" />
</Button>

How can I get this button in the AdornerLayer to use the image's native resolution, instead of stretching with the ViewBox?

Nathan
  • 10,593
  • 10
  • 63
  • 87

2 Answers2

1

Basically, the idea is to get the transform from the ViewBox and apply the inverse, with a binding converter.

The binding converter can get the inverse with the following code:

((ContainerVisual)VisualTreeHelper
.GetChild((System.Windows.DependencyObject)viewbox, 0)).Transform.Inverse

In xaml, the binding would look something like

<Button.LayoutTransform>
    <MultiBinding Converter="{bc:ExemptFromViewboxTransformConverter}">
       <Binding Source="{x:Reference Name=viewboxName}" />
       <!-- The ActualWidth/ActualHeight bindings are only used to ensure 
            the transform updates when the window is resized. -->
        <Binding Source="{x:Reference Name=viewboxName}" Path="ActualWidth" />
        <Binding Source="{x:Reference Name=viewboxName}" Path="ActualHeight" />
    </MultiBinding>
</Button.LayoutTransform>

See also:

Community
  • 1
  • 1
Nathan
  • 10,593
  • 10
  • 63
  • 87
0

You can disable viewbox scaling using this solution: Disable Viewbox Resize for Specific Elements

Basically, add a handler for ViewBox_SizeChanged and set the exempt element's transform to the inverse of the ViewBox's transform.

Then in your XAML, add ViewBoxExtra.DisableScaling="true", e.g.

<Rectangle Width="50" Height="50" local:ViewBoxExtra.DisableScaling="true"/>
philu
  • 795
  • 1
  • 8
  • 17