我创建了一个自定义按钮,上面有一个布尔标志,表示它是否被选中。如果选中它,我想更改按钮的背景颜色,以便用户可以快速了解他们正在查看的控件。问题是我无法让触发器正常工作。我在这里看到的一切,我已经尝试但在代码的InitializeComponent部分仍然出现错误。
以下是我正在处理的属性
Private mIsSelected As Boolean = False
Public Property IsSelected() As Boolean
Get
Return mIsSelected
End Get
Set(ByVal value As Boolean)
mIsSelected = value
End Set
End Property
Public Shared ReadOnly MyBackgroundProperty As DependencyProperty = DependencyProperty.Register("MyBackground",
GetType(LinearGradientBrush), GetType(RentAdjButtons),
New PropertyMetadata(New LinearGradientBrush(ColorConverter.ConvertFromString("#3366CC"), ColorConverter.ConvertFromString("#0000FF"), New System.Windows.Point(0.5, 0), New System.Windows.Point(0.5, 1))))
Private mMyBackground As New LinearGradientBrush
Public Property MyBackground() As LinearGradientBrush
Get
Return mMyBackground
End Get
Set(ByVal value As LinearGradientBrush)
mMyBackground = value
End Set
End Property
这是我的XMAL
<Style TargetType="Com:RentAdjButtons">
<Setter Property="MyBackground" Value="{StaticResource DefaultBackground}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="MyBackground" Value="{StaticResource SelectedButton}" />
</Trigger>
</Style.Triggers>
</Style>
目前我得到的错误是“触发器上的属性不能为空”
提前感谢您的任何帮助
答案 0 :(得分:1)
您可以使用默认情况下具有此属性的ToggleButton。
答案 1 :(得分:0)
Trigger.Property
必须是DependencyProperty
才能使触发器正常工作。有关文档,请参阅MSDN。您需要将类属性IsSelected
更改为DependencyProperty
。
此外,MyBackground的getter / setter应该使用SetValue
和GetValue
{/ 3}}在MSDN中进行定义:
Public Property State() As Boolean
Get
Return CType(Me.GetValue(StateProperty), Boolean)
End Get
Set(ByVal value As Boolean)
Me.SetValue(StateProperty, value)
End Set
End Property
Public Shared ReadOnly StateProperty As DependencyProperty =
DependencyProperty.Register("State", GetType(Boolean), GetType(MyStateControl),
New PropertyMetadata(False))