StealthRT StealthRT - 18 days ago 8
Vb.net Question

Bind background color to all textboxes

Hey all I am new at WPF and was wondering if anyone would show me how to define a binding color so that I can add that same code to all my textboxes and only have to change one code instead of each individually?

My XAML code:

<TextBox x:Name="txtBC_Copy"
materialDesign:HintAssist.Hint="Enter the items name that was scanned in"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
Margin="478,90,25,618"
FontSize="24"
Background="{Binding MyBackgroundColor}"
BorderBrush="#890C00FF"
FontWeight="Bold"
CaretBrush="#89000000"
BorderThickness="0,0,0,2"
Foreground="#DD000000" >
<TextBox.SelectionBrush>
<SolidColorBrush Color="#890C00FF" Opacity="0"/>
</TextBox.SelectionBrush>
</TextBox>


As you can see above I already tried Binding MyBackgroundColor.

The code behind:

Private _myBackgroundColor As Color
Public Property MyBackgroundColor() As Color
Get
Return _myBackgroundColor
End Get
Set
If Value <> _myBackgroundColor Then
_myBackgroundColor = Value
End If
End Set
End Property

Public Sub New()
InitializeComponent()
MyBackgroundColor = Colors.Red
End Sub


Using both of those above, I run the app and I do not see a red background on the txtBC_Copy textbox.... What am I missing?

Answer

Type of Background is Brush, so, change your code like :

Private _myBackgroundColor As Brush
Public Property MyBackgroundColor() As Brush
    Get
        Return _myBackgroundColor
    End Get
    Set
        If Value <> _myBackgroundColor Then
            _myBackgroundColor = Value
        End If
    End Set
End Property

Public Sub New()
    InitializeComponent()
    MyBackgroundColor = Brushes.Red
End Sub
Comments