wpf - 带有背景图像和颜色的文本框

标签 wpf xaml textbox

我有一个 TextBox 控件,我希望能够设置背景图像和背景颜色。

目前我可以设置一个,或者另一个,但不能同时设置。当我尝试同时设置两者时,我收到“多次设置属性‘背景’”错误。

这是我使用的代码:

<TextBox Name="tbImageTextBox">
      <TextBox.Background>
           <ImageBrush ImageSource="/Resources/Images/image.png" 
                  AlignmentX="Right" Stretch="None"/>
           <SolidColorBrush>#FF8D8A8A</SolidColorBrush>
      </TextBox.Background>
</TextBox>

我还尝试在 <TextBox.Background> 中为 TextBox 和图像设置样式中的背景颜色, 但颜色被忽略。

最佳答案

根据需要使用网格资源作为背景。同一资源可用于多个文本框。

<Grid>
    <Grid.Resources>
        <ImageBrush x:Key="img" ImageSource="Blue hills.jpg"></ImageBrush>
        <SolidColorBrush x:Key="brownBrush" Color="Brown"></SolidColorBrush>
    </Grid.Resources>

    <TextBox x:Name="test" Background="{StaticResource img}" Width="100" Height="40" />
</Grid>

关于wpf - 带有背景图像和颜色的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12180146/

相关文章:

c# - WPF - WrapPanel/Grid 中的 ItemsControl 数据绑定(bind)

c# - WPF 中的文化更改后更新资源字符串(GUI 中的文本)

ASP.NET:带有文本框的复选框列表?

c# - 将新设备添加到 xaml 布局设计器 Visual Studio 2015 UWP

c# - 修复 xaml 文件中的大小

c# - Windows Phone 文本框以只读模式显示插入符

textbox - 在ckeditor图像属性窗口中添加新的输入文本框

.net - WPF 控件可以重复使用吗?

wpf - Silverlight 不支持 XmlNode : re-implement or use string parsing?

c# - 如何在 WPF 中创建抽象类系统控件的实例?