I have a custom control that I want to style:
It is just a class that inherits from TextBox and another Interface, the interface only adds an extra property.
How can I apply a style to this custom control so that when the read only property is set, the background turns gray?
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
}
Above is the code for the entire class, the way I used the style seemed like the appropriate way but when I add this control to the designer I get the following error:
Triggers collection members must be of type EventTrigger.
Can anyone point me in the right direction?
You can redefine dependency properties’ default behavior, and in particular, you can define
PropertyChangedCallbacks:Please be careful:
UIPropertyMetadata!=FrameworkPropertyMetadata