WPF Border添加背景图片


        <Border  Name="border_download" BorderThickness="2" BorderBrush="White" CornerRadius="5">
            <Border.Background>
                <ImageBrush ImageSource="Images\3x.jpg"></ImageBrush>
            </Border.Background>
        </Border>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中,可以通过设置TextBox控件的水印文本来实现添加背景字符。 以下是一个示例: ```xaml <TextBox Text="Enter text here..." Foreground="Gray" IsEnabled="{Binding IsTextBoxEnabled}" Style="{StaticResource MyTextBoxStyle}"/> ``` 其中,Text属性设置了文本框中默认的提示文本,Foreground属性设置了提示文本的颜色。IsEnabled属性用于控制文本框是否可编辑。 另外,还需要定义一个样式来设置提示文本的样式: ```xaml <Style x:Key="MyTextBoxStyle" TargetType="TextBox"> <Style.Resources> <VisualBrush x:Key="WatermarkBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None"> <VisualBrush.Visual> <TextBlock Text="Enter text here..." Foreground="Gray"/> </VisualBrush.Visual> </VisualBrush> </Style.Resources> <Setter Property="Background" Value="White"/> <Setter Property="BorderBrush" Value="Gray"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Padding" Value="2"/> <Setter Property="Margin" Value="5"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Grid> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True"/> <Border Background="{StaticResource WatermarkBrush}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True" Opacity="{Binding Path=Text.IsEmpty, Converter={StaticResource BooleanToVisibilityConverter}}"> </Border> <ScrollViewer Margin="0" x:Name="PART_ContentHost"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger> <Trigger Property="IsKeyboardFocused" Value="True"> <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 在样式中,定义了一个VisualBrush作为提示文本的背景,使用了一个TextBlock作为VisualBrush的Visual元素,设置了文本和颜色。在控件模板中,使用了两个Border元素,第一个用于显示文本框的背景和边框,第二个用于显示提示文本,通过Opacity属性来控制其显示和隐藏。最后,在控件模板中添加了几个触发器来处理文本框的状态变化。 通过上述方式,就可以在WPF中实现文本框添加背景字符的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值