キャンバスの親のサイズを動的に変更するときにサイズを変更したい垂直線と水平線があります。(ランドマーク)
水平線を常にキャンバスの左右の境界線から 25 離し、下の境界線から 13 離したいと思います。
垂直線についても同様で、上下の境界線から 25、左の境界線から 13 離れています。
簡単な解決策はありますか?キャンバスを別のコントロールに変更する必要がありますか?
キャンバス上のグリッドに線を貼り付けるだけで、正しい動作が得られます
<Grid Width="600" Height="600">
<Canvas Background="LightBlue">
// stuff here
</Canvas>
<Grid>
<Rectangle Fill="Black" Height="1"
Stroke="Black" VerticalAlignment="Bottom" Margin="25,0,25,13"/>
<Rectangle Fill="Black"
HorizontalAlignment="Left" Stroke="Black" Width="1" Margin="13,25,0,25"/>
</Grid>
</Grid>
オブジェクトの高さ、幅、位置を設定するために、あなたのとにConverters
基づいて使用しますActualHeight
ActualWidth
Canvas
Line
個々のコンバーターをたくさん書くのを避けるために、すべての計算に使用できるMathConverterをブログに投稿しています。
<Canvas x:Name="MyCanvas">
<!-- Horizontal Line: 25 from each side, and 13 from bottom -->
<!-- May need to adjust the Canvas.Top ConverterParameter based on Line height -->
<Line Height="1"
Canvas.Left="25"
Canvas.Top="{Binding ElementName=MyCanvas, Path=ActualHeight,
Converter={StaticResource MathConverter},
ConverterParameter=@VALUE-14}"
Width="{Binding ElementName=MyCanvas, Path=ActualWidth,
Converter={StaticResource MathConverter},
ConverterParameter=@VALUE-50}" ... />
<!-- Vertical Line: 25 from top and bottom, and 13 from left -->
<Line Canvas.Left="13" Canvas.Top="25"
Height="{Binding ElementName=MyCanvas, Path=ActualHeight,
Converter={StaticResource MathConverter},
ConverterParameter=@VALUE-50}" ... />
</Canvas>
これらはすべてバインディングであるため、バインドされたプロパティが変更されるたびに更新されます (MyCanvas.ActualHeight
およびMyCanvas.ActualWidth
)
を設定する必要がある場合Grid
に代わりに使用します。Canvas
Margin
行に境界線からのスペースを確保するには、[プロパティ] に移動しMargin
、[レイアウト エリア] で使用してスペースを設定します。水平線を に、HorizontalAlignment を に設定VerticalAlignment
しBottom
ますStretch
。この場合の証拠金とします25,0,25,13
。
垂直線のをにに設定VerticalAlignment
します。マージンはStretch
HorizontalAlignment
Left
13,25,0,25
運がいい