Home > database >  How to change margin of Textbox inside VisualBrush?
How to change margin of Textbox inside VisualBrush?

Time:12-08


I came across this method while looking for the Textbox watermark feature.

<TextBox Name="txtBoxName" Width="120" Height="23">
      <TextBox.Resources>
        <VisualBrush x:Key="HelpBrush" TileMode="None" Opacity="0.3" Stretch="None" AlignmentX="Left">
          <VisualBrush.Visual>
            <TextBlock FontStyle="Italic" Text="Watermark effect"/>
          </VisualBrush.Visual>
        </VisualBrush>
      </TextBox.Resources>
      <TextBox.Style>
        <Style TargetType="TextBox">
          <Setter Property="Height" Value="23"/>
          <Setter Property="HorizontalAlignment" Value="Left"/>
          <Setter Property="VerticalAlignment" Value="Top"/>
          <Style.Triggers>
            <Trigger Property="Text" Value="{x:Null}">
              <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
            </Trigger>
            <Trigger Property="Text" Value="">
              <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
            </Trigger>
          </Style.Triggers>
        </Style>
      </TextBox.Style>
    </TextBox>

I want to control the margin of a textblock inside a VisualBrush.
If you set the margin inside, it will not change.
How can you move it?
Thanks.

enter image description here

CodePudding user response:

place TextBlock into some container, e.g. Grid

<VisualBrush.Visual>
    <Grid Background="Transparent">
        <TextBlock FontStyle="Italic" Text="Watermark effect" Margin="15,0"/>
    </Grid>
</VisualBrush.Visual>
  • Related