I tried to create a custom control, having a semitransparent rounded background:
<Canvas>
<TextBlock x:Name="StopText" Text="Some test text"/>
<Rectangle Fill="SkyBlue"
Width="{Binding Source=StopText, Path=ActualHeight}"
Height="20"
RadiusX="5" RadiusY="5" Opacity="0.2"/>
</Canvas>
The problem is that I can’t, probably, bind to the ActualHeight/ActualWidth properties, cause they are not dependency ones.
What to do to mantain the rectangle and textbox of same size?
The correct binding is to use
ElementName, notSource, when binding to another element:Also, you do realize that you are binding the width of the
Rectangleto theHeightof theTextBlock, right?If this is really the way you want to set up your control, you will want to bind the
Rectangle‘s Width to theTextBlock‘sActualWidth, andHeighttoActualHeight.UPDATE
Per the comments below, here is an implementation using a
Gridwith no binding:GridandCanvasuse different layout systems, and since you aren’t using the functionality theCanvasprovides,Gridis the better choice.The big difference in the child elements is that the
Rectanglenow just uses Horizontal andVerticalAlignmenttoStretchacross the entireGrid, instead of worrying about the sizes of anything.