I’m trying to make some kind of shape in wpf, which does resize itself to the content (which should be text). Unfortunately, the stretch property isn’t the right thing, since I want only the width of the Shape resized and without the borders (pls copy bottom example in xamlpad to see for yourself) of this shape stretched. The borders should stay the way they are, or at least scale in Uniform.
I’ve tried many ideas. With different slices of the shape in a grid, stackpanel, or with a clipped panel and etc.
My next approach would be the following one:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Page.Resources>
<LinearGradientBrush StartPoint="0.0,1" EndPoint="0.0,0" x:Key="brushYellow">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0.000000" Color="#fffef4a6"/>
<GradientStop Offset="0.175824" Color="#fffef9d6"/>
<GradientStop Offset="0.800000" Color="#fffef9d6"/>
<GradientStop Offset="1.000000" Color="#fffef4a6"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush></Page.Resources><Grid>
<Path Stroke="#fffce90d" StrokeThickness="1" Fill="{StaticResource brushYellow}">
<Path.Data>
<CombinedGeometry GeometryCombineMode="Exclude">
<CombinedGeometry.Geometry1>
<RectangleGeometry RadiusX="15" RadiusY="15">
<!--RectangleGeometry.Rect>
<Binding StringFormat="{}{0 0 {0} 82}" ElementName="Text" Path="Width"/>
</RectangleGeometry.Rect-->
<RectangleGeometry.Rect>
<Rect Width="150" Height="82"/>
</RectangleGeometry.Rect>
</RectangleGeometry>
</CombinedGeometry.Geometry1>
<CombinedGeometry.Geometry2>
<PathGeometry>
<PathGeometry.Figures>
<PathFigureCollection>
<PathFigure IsClosed="True" StartPoint="0,15">
<PathFigure.Segments>
<PathSegmentCollection>
<LineSegment Point="17,41" />
<LineSegment Point="0,67" />
</PathSegmentCollection>
</PathFigure.Segments>
</PathFigure>
</PathFigureCollection>
</PathGeometry.Figures>
</PathGeometry>
</CombinedGeometry.Geometry2>
</CombinedGeometry>
</Path.Data>
</Path>
<TextBox Name="Text" Background="Transparent" BorderThickness="0" MinWidth="150" Margin="0"/>
</Grid></Page>
This will work right out of the box in xamlpad. The uncommented part at line 19 is what I really want to achieve: Binding the Rect of the Rectangle to something else. Unfortunately, the Width of Rect isn’t dp, that’s why I’m using directly a stringformatted Binding to Rect itself.
As expected with life, that doesn’t work (nothing is visible) 😀
What am I doing wrong here?
I use a set of classes named ViewboxPath, ViewboxLine, ViewboxPolyline, etc that change the stretch semantics of Shape to be a little more tractable. I’m not sure I understood your question, so I don’t know if my technique will solve your problem or not.
As I read it, either you want control over the stretching, which this solution will provide, or you want to have the strokes stretch along with the image, which Sam’s answer will provide.
Anyhow, below is the code for these classes and this is how you use them:
My Viewbox shape classes are used just like normal shapes (
Polyline,Polygon,PathandLine) except for the extraViewboxparameter, and the fact that they default toStretch="Fill". The Viewbox parameter specifies, in the coordinate system used to specify the shape, the area of the geometry that should be stretched usingFill,UniformorUniformToFillsettings, instead of usingGeometry.GetBounds.This gives very precise control over the stretching and makes it easy to have separate shapes align accurately with one another.
Here is the actual code for my Viewbox shape classes, including the abstract base class
ViewboxShapethat contains common functionality:Enjoy!