Reputation: 1327
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?
Upvotes: 1
Views: 540
Reputation: 16628
You can redefine dependency properties' default behavior, and in particular, you can define PropertyChangedCallback
s:
public class DionysusTextBox : TextBox, IDionysusControl
{
static DionysusTextBox()
{
//For the IsReadOnly dependency property
IsReadOnlyProperty.OverrideMetadata(
//On the type DionysusTextBox
typeof(DionysusTextBox),
//Redefine default behavior
new FrameworkPropertyMetadata(
//Default value, can also omit this parameter
null,
//When IsReadOnly changed, this is executed
new PropertyChangedCallback(
(dpo, dpce) =>
{
//dpo hold the DionysusTextBox instance on which IsReachOnly changed
//dpce.NewValue hold the new value of IsReadOnly
//Run logic to set the background here, you are on the UI thread.
//Example of setting the BorderBrush from ARGB values:
var dioBox = dpo as DionysusTextBox;
//Should always be true, of course, it's just my OCD ;)
if (dioBox != null)
{
dioBox.BorderBrush =
ColorConverter.ConvertFromString("#FFDDDDDD") as Color?;
}
})));
//For the BorderBrush property
BorderBrushProperty.OverrideMetadata(
//On the type DionysusTextBox
typeof(DionysusTextBox),
//Redefine default behavior
new FrameworkPropertyMetadata(
//Default value
ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));
}
public DionysusTextBox()
{
SetStyle();
}
}
Please be careful: UIPropertyMetadata
!= FrameworkPropertyMetadata
Upvotes: 4