WPF Constrain the resize of a canvas' child object to the dimensions of the canvas

Posted by Scott on Stack Overflow See other posts from Stack Overflow or by Scott
Published on 2010-04-09T20:10:15Z Indexed on 2010/04/09 20:13 UTC
Read the original article Hit count: 752

Filed under:
|
|

Given the following XAML:

<Window x:Class="AdornerTesting.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="500" Width="500"
        Loaded="Window_Loaded">
    <Grid Name="grid">
        <Canvas Name="canvas" Width="400" Height="400" Background="LightGoldenrodYellow">
            <RichTextBox Name="richTextBox" Canvas.Top="10" Canvas.Left="10" BorderBrush="Black" BorderThickness="2"
                     Width="200"
                     Height="200"
                     MaxWidth="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}},Path=ActualWidth}"
                     MaxHeight="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Canvas}},Path=ActualHeight}"/>
        </Canvas>
    </Grid>
</Window>

and a set of adorners being added to the RichTextBox in the Loaded event like so:

AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(richTextBox);
adornerLayer.Add(new ResizeAdorner(richTextBox));

How do I keep from being able to resize the RichTextBox beyond the visble bounds of the Canvas?

The ResizeAdorner is essentially the same code that can be found in the MSDN adorner example and it works just fine. Should I be doing something with the bindings of MaxWidth and MaxHeight in the code-behind to calculate how the RichTextBox can be resized? Or is there a way to do this in XAML?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml