我有一个要设置样式的自定义控件:
它只是一个继承自 TextBox 和另一个 Interface 的类,该接口只是增加了一个额外的属性。
如何将样式应用于此自定义控件,以便在设置只读属性时,背景变为灰色?
public class DionysusTextBox : TextBox, IDionysusControl
{
public DionysusTextBox()
{
SetStyle();
}
#region IDionysusControl Members
public bool KeepReadOnlyState
{
get { return (bool)GetValue(KeepReadOnlyStateProperty); }
set { SetValue(KeepReadOnlyStateProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty KeepReadOnlyStateProperty =
DependencyProperty.Register("KeepReadOnlyState", typeof(bool), typeof(DionysusTextBox), new UIPropertyMetadata(true));
#endregion
#region Style
Style styleListBoxItem = new Style(typeof(DionysusTextBox));
Trigger triggerReadonly = new Trigger { Property = DionysusTextBox.IsReadOnlyProperty, Value = true };
private void SetStyle()
{
triggerReadonly.Setters.Add(new Setter(DionysusTextBox.BackgroundProperty, Brushes.Black));
this.Triggers.Add(triggerReadonly);
}
#endregion
}
以上是整个类的代码,我使用样式的方式似乎是合适的方式,但是当我将此控件添加到设计器时,我收到以下错误:
Triggers collection members must be of type EventTrigger.
谁能指出我正确的方向?