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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12180146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:08:58  来源:igfitidea点击:

TextBox with background image and color

wpfxamltextbox

提问by binncheol

I have a TextBox control and I would like to be able to set a background image and a background color.

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

Currently I can set one, or the other but not both. When I try to set both simultaneously I receive a "The property 'Background' is set more than once" error.

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

Here is the code I used:

这是我使用的代码:

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

I have also attempted to set the background color in the style for the TextBox and the image in the <TextBox.Background>, but the color is ignored.

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

回答by Hasan Zubairi

Use the grid resource for background as needed. Same resource can be used for multiple textboxes.

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

<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>

回答by binncheol

I ended up putting the TextBox into a grid with the Background color set and applying the background image to the TextBox itself as using VisualBrush and DrawingBrush stretched my image or only applied the background color to the image - not the rest of the TextBox.

我最终将 TextBox 放入设置了背景颜色的网格中,并将背景图像应用到 TextBox 本身,因为使用 VisualBrush 和 DrawingBrush 拉伸我的图像或仅将背景颜色应用到图像 - 而不是 TextBox 的其余部分。

回答by H.B.

You will need to combine the colour and the image in a single Brushinstance, you could use a DrawingBrushor a VisualBrushcontaining an Imagecontrol with your image and the Backgroundset to the colour for example.

您需要的颜色和图像中的单个结合Brush实例,你可以使用一个DrawingBrush或一个VisualBrush包含Image与图像控制和Background设定的颜色例如。

回答by Dan Puzey

You probably want either a VisualBrushor a DrawingBrush. More information on those can be found at MSDN here. Something like this might get you started:

您可能想要 aVisualBrushDrawingBrush. 可以在 MSDN此处找到有关这些的更多信息。像这样的事情可能会让你开始:

<Rectangle Width="75" Height="75">
  <Rectangle.Fill>
    <VisualBrush TileMode="Tile">
      <VisualBrush.Visual>
        <Grid>
          <Image BaseUri="somepic.png" />
          <Rectangle Brush="FF8D8A8A" />  
        </Grid>
      </VisualBrush.Visual>
    </VisualBrush>
  </Rectangle.Fill>
</Rectangle>