考虑下面的XAML的尺寸:WPF约束画布的大小调整子对象到画布
<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>
和一组装饰器被添加到RichTextBox中的Loaded事件,像这样:
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(richTextBox);
adornerLayer.Add(new ResizeAdorner(richTextBox));
我该如何防止在Canvas的可见范围之外调整RichTextBox的大小?
ResizeAdorner本质上是可以在MSDN adorner示例中找到的相同的代码,它工作得很好。我是否应该在代码隐藏中使用MaxWidth和MaxHeight的绑定来计算RichTextBox的可调整大小?或者有没有办法在XAML中做到这一点?
是,在0改变控制的初始位置, 0在canva上s会将拖动向右和向下拖到画布的边缘。我当时遇到的问题以及我原来的问题中没有提到的,就是左右调整大小。不知道我怎么没有把这个问题放在最初的问题上,但我没有。事实证明,这是我在那些被责怪的方向调整大小的代码。感谢XAML绑定的提示,绑定的东西有时会抛出我。 – Scott 2010-04-15 17:07:13