嘿,我是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>
如上所示,我已经尝试绑定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文本框中看不到红色背景......我缺少什么?
答案 0 :(得分:1)
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