Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not aware of any built-in functionality that can do this, but you could write a custom control to do so. </p> <p>The salient part of such a control would be a 9 part grid in which 4 parts were of fixed size (the corners), two parts had fixed heights and variable widths (center top and center bottom), two parts had fixed widths and variable heights (left center and right center), and the final part had variable height and width (the middle). Stretching in only one direction (e.g. making a button that only grows horizontally) is as simply as limiting the middle portion's height.</p> <p>In XAML, that would be:</p> <pre><code>&lt;Grid&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width="20"/&gt; &lt;ColumnDefinition Width="*"/&gt; &lt;ColumnDefinition Width="20"/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="20"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;RowDefinition Height="20"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;/Grid&gt; </code></pre> <p>Then you'd add objects to paint the images on to (I'll use Rectangles), as well as an object to put content into (ContentPresenter):</p> <pre><code>&lt;Rectangle Grid.Row="0" Grid.Column="0" x:Name="TopLeft"/&gt; &lt;Rectangle Grid.Row="0" Grid.Column="1" x:Name="TopCenter"/&gt; &lt;Rectangle Grid.Row="0" Grid.Column="2" x:Name="TopRight"/&gt; &lt;Rectangle Grid.Row="1" Grid.Column="0" x:Name="CenterLeft"/&gt; &lt;Rectangle Grid.Row="1" Grid.Column="2" x:Name="CenterRight"/&gt; &lt;Rectangle Grid.Row="2" Grid.Column="0" x:Name="BottomLeft"/&gt; &lt;Rectangle Grid.Row="2" Grid.Column="1" x:Name="BottomCenter"/&gt; &lt;Rectangle Grid.Row="2" Grid.Column="2" x:Name="BottomRight"/&gt; &lt;Grid Grid.Row="2" Grid.Column="1" x:Name="Middle"&gt; &lt;Rectangle/&gt; &lt;ContentPresenter x:Name="MiddleContent"/&gt; &lt;/Grid&gt; </code></pre> <p>Each of the Rectangles can be painted using an ImageBrush so that they show the correct portion of your source image:</p> <pre><code>&lt;Rectangle&gt; &lt;Rectangle.Fill&gt; &lt;ImageBrush ImageSource="Image.png" TileMode="None" &lt;!-- Add the settings necessary to show the correct part of the image --&gt; /&gt; &lt;/Rectangle.Fill&gt; &lt;/Rectangle&gt; </code></pre> <p>Wrapping all of that up into a custom control, you could produce a pretty usable 9-slice image control:</p> <pre><code>&lt;local:NineSliceImage Image="Source.png" Slice="20,20"&gt; &lt;TextBox Text="Nine Slice Image TextBox!"/&gt; &lt;/local:NineSliceImage&gt; </code></pre> <p>Where Slice is a property of type System.Windows.Size, so that you can use it like the Margin/Padding/etc. properties for setting the position of the slices.</p> <p>You'll also want to set SnapToDisplayPixels to True on all of the Rectangles; otherwise, you'll see small gaps between the pieces of the image at certain resolutions as WPF tries to interpolate the in-between pixels.</p> <p>An alternative, slightly faster way of doing this if you plan to use a lot of these controls is to override OnRender and do it there; I've done this in the past for a 3-slice image control, but it's quite a bit more tricky.</p> <p>That should get you most of the way there - if there's anything I'm missing, leave a comment.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload