0

In WPF, I have a TextBox with a BackgroundProperty set to a custom brush. I want to give this brush some kind of visual buffer from the border of the TextBox. The brush is a GlyphRunBrush which acts much like an ImageBrush, but has a rasterized glyph run as the brush source.

How can I do this?

Example: enter image description here

codekaizen
  • 26,990
  • 7
  • 84
  • 140

1 Answers1

1

One way would be to apply a transformation to the brush itself, like so:

<TextBox>
    <TextBox.Background>
        <ImageBrush ImageSource="/MyApp;component/Search.ico"
                    AlignmentX="Right" Stretch="Uniform">
            <ImageBrush.Transform>
                <TransformGroup>
                    <TranslateTransform X="-5"/>
                </TransformGroup>
            </ImageBrush.Transform>
        </ImageBrush>
    </TextBox.Background>
</TextBox>

You could also try changing the width on the brush's Viewport property, but since you're aligning it on the right side, that may be more complicated than it needs to be.

YotaXP
  • 3,844
  • 1
  • 22
  • 24
  • Nice trick. A combination of a `ScaleTransform` with 0.8 scale factor and a `TranslateTransform` scaled and bound to the width of the textbox to keep the glyph to the right worked. – codekaizen Dec 04 '12 at 06:50