将背景颜色绑定到所有文本框

Bind background color to all textboxes

大家好,我是 WPF 的新手,想知道是否有人会告诉我如何定义绑定颜色,以便我可以将相同的代码添加到我的所有文本框,而只需更改一个代码而不是单独更改每个代码?

我的XAML代码:

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

正如您在上面看到的,我已经尝试了 Binding MyBackgroundColor

背后的代码:

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

使用上面的两个,我 运行 应用程序并且在 txtBC_Copy 文本框上没有看到红色背景....我错过了什么?

Background 的类型是 Brush,因此,将您的代码更改为:

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