我有一个 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/