Set Background Color of Wpf Textbox in C# Code

Set background color of WPF Textbox in C# code

textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;

WPF Foreground and Background is of type System.Windows.Media.Brush. You can set another color like this:

using System.Windows.Media;

textBox1.Background = Brushes.White;
textBox1.Background = new SolidColorBrush(Colors.White);
textBox1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
textBox1.Background = System.Windows.SystemColors.MenuHighlightBrush;

How to change Background color of TextBox?

You need to use Brushes:

txtStatus.Foreground = Brushes.White;

It contains a lot of colors, though if you want to use aRGB values then you can do it like this:

txtStatus.Foreground = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

Change Background Color for WPF textbox in changed-state

Just use a MultiBinding with the same property twice but have Mode=OneTime on one of the bindings. Like this:

Public Class MVCBackground
Implements IMultiValueConverter

Public Function Convert(ByVal values() As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
Static unchanged As Brush = Brushes.Blue
Static changed As Brush = Brushes.Red

If values.Count = 2 Then
If values(0).Equals(values(1)) Then
Return unchanged
Else
Return changed
End If
Else
Return unchanged
End If
End Function

Public Function ConvertBack(ByVal value As Object, ByVal targetTypes() As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class

And in the xaml:

<TextBox Text="{Binding TestText}">
<TextBox.Background>
<MultiBinding Converter="{StaticResource BackgroundConverter}">
<Binding Path="TestText" />
<Binding Path="TestText" Mode="OneTime" />
</MultiBinding>
</TextBox.Background>
</TextBox>

No extra properties or logic required and you could probably wrap it all into your own markup extension. Hope that helps.

WPF. Change Background color TextBlock if text in TextBlock is equal text in ComboBox

I did it so way:

<Border BorderBrush="#FF757576" BorderThickness="0, 0, 0, 1">
<StackPanel x:Name="StackPanelDevice" Orientation="Horizontal" >
<TextBlock Text="{Binding Path=DeviceName}" HorizontalAlignment="Left" Width="200" >
<TextBlock.Style>
<Style>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource multiValueEqualityConverter}">
<Binding ElementName="ComboBoxModels" Path="SelectedValue"/>
<Binding Path="DeviceName"/>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBlock Text="{Binding Path=Count}" HorizontalAlignment="Right" Width="30" />
</StackPanel>
</Border>

Sample Image

want to change background of textbox based on the input of another textbox wpf

I am sharing a sample by trigger . With Converter you can pass the parameters with Element Name property as well.

 <StackPanel>
<TextBlock Height="20" Name="colorTb" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="Red"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=textTb, Path=Text}" Value="Process">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
<TextBox Height="20" Name="textTb" Text="xyz" >

</TextBox>
</StackPanel>

This is with converter

<StackPanel>
<StackPanel.Resources>
<converters:ColorConverter x:Key="converter1"></converters:ColorConverter>
</StackPanel.Resources>
<TextBox Height="20" Name="colorTb" Background="{Binding ElementName=textTb, Path=Text ,Converter={StaticResource converter1}}" >

</TextBox>
<TextBox Height="20" Name="textTb" Text="xyz" >

</TextBox>
</StackPanel>

And Converter Code Be like

public class ColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var backColor = Brushes.Transparent;
if (value!=null && value.ToString().Equals("Process"))
{
backColor = Brushes.Green;
}
return backColor;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

How to change TextBox's Background color?

If it's WPF, there is a collection of colors in the static class Brushes.

TextBox.Background = Brushes.Red;

Of course, you can create your own brush if you want.

LinearGradientBrush myBrush = new LinearGradientBrush();
myBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
myBrush.GradientStops.Add(new GradientStop(Colors.Orange, 0.5));
myBrush.GradientStops.Add(new GradientStop(Colors.Red, 1.0));
TextBox.Background = myBrush;

WPF set Textbox Border color from C# code

textBox.BorderBrush = System.Windows.Media.Brushes.Red;

Works for me, make sure you're not using the System.Drawing.Brushes, you need to use the Windows.Media brush instead.



Related Topics



Leave a reply



Submit