wpfsilverlightimagebrush

Binding a Canvas size to the image size in its background ImageBrush


I have a canvas that I want sized to fit a background ImageBrush.

One hurdle here is that the control is being instantiated and then the image is being passed in and assigned as the ImageBrush. The other hurdle is that I'm making this control for both WPF and Silverlight and the ImageBrush is different (i.e. they share no events).

Edit:

<UserControl x:Class="Escalon.Annotations.AnnotationControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="400" Width="500" >
    <ScrollViewer Name="ctlScrollViewer" HorizontalScrollBarVisibility="Auto" Height="400" Width="500">
        <Canvas Name="ctlSizingCanvas" >
            <Canvas  Name="ctlCanvas" Loaded="ctlCanvas_Loaded" Height="400" Width="500"
                MouseLeftButtonDown="MouseLeftButtonDownHandler" MouseMove="Canvas_MouseMove" MouseLeftButtonUp="Canvas_MouseLeftButtonUp" >
                <Canvas.RenderTransform>
                    <ScaleTransform x:Name="ctlZoomTransform" />
                </Canvas.RenderTransform>
                <Canvas.Background>
                    <ImageBrush ImageSource="{Binding Source}" Stretch="UniformToFill" ></ImageBrush>
                </Canvas.Background>
            </Canvas>
        </Canvas>
    </ScrollViewer>
</UserControl>

The problem is, i'm not using an Image control, I'm using an ImageBrush control, which doesn't have height and width.


Solution

  • Instead of using ImageBrush, I just added the image to the canvas and bound the Canvas size to the Image size.