wpf 半透明背景的文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14984933/
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 07:38:42  来源:igfitidea点击:

Textbox with Semi-Transparent Background

wpfxaml

提问by dongx

Semi-Transparent background of the Textbox is needed, and the text content should be shown as normal.

需要Textbox的半透明背景,文本内容正常显示。

Styleor Brushwhich can store in the Resource dictionary is good.

可以存储在资源字典中的样式画笔是好的。

NOTE:

笔记:

  1. My textBox is wrapped within a ContentControl.

  2. This similar question does not help. TextBox with a Transparent Background .

  1. 我的 textBox 包含在 ContentControl 中。

  2. 这个类似的问题没有帮助。具有透明背景的文本框

回答by kmatyaszek

In XAML you can set Backgroundproperty to Transparent:

在 XAML 中,您可以将Background属性设置为Transparent

<TextBox Background="Transparent" />

In code-behind you can use following code:

在代码隐藏中,您可以使用以下代码:

TextBox tb = new TextBox 
{
    Width = 100,
    Background = Brushes.Transparent
};

If you want to set background to transparent to all TextBoxyou can use following style:

如果要将背景设置为对所有人透明,则TextBox可以使用以下样式:

<Style TargetType="TextBox">
    <Setter Property="Background" Value="Transparent" />
</Style>

回答by Constanta

If you want to set a semi-transparent background in code-behind you can do this

如果你想在代码隐藏中设置半透明背景,你可以这样做

use a dependency prop on a class that inherits from TextBox

在继承自 TextBox 的类上使用依赖项道具

public static readonly DependencyProperty BgColourProperty =
  DependencyProperty.Register("BgColour", typeof(SolidColorBrush), typeof(myTextBox), null);

public SolidColorBrush BgColour
    {
        get { return (SolidColorBrush)GetValue(BgColourProperty); }
        set { SetValue(BgColourProperty, value); }
    }

then set whatever colour you wish using Color.FromArgb() where 1st argument is Alpha component

然后使用 Color.FromArgb() 设置您想要的任何颜色,其中第一个参数是 Alpha 组件

myTextBox.BgColour = new SolidColorBrush(Color.FromArgb(120,240, 17, 17));