Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8741093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:14:48+00:00 2026-06-13T11:14:48+00:00

I have a custom control that I want to style: It is just a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:14:49+00:00Added an answer on June 13, 2026 at 11:14 am

    You can redefine dependency properties’ default behavior, and in particular, you can define PropertyChangedCallbacks:

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control that inherits from .NET's CompositeControl class. This control overrides
I have a custom control that inherits from UserControl , and I can't get
In WPF, I have a custom control that inherits from TreeView. The code is
I'm creating a custom control and I want to have style templates that can
I have a custom control that is derived from TabItem , and I want
I want to have a custom control that works on whatever documents I decide
I have a Custom Control that has multiple textbox fields and a checkbox contained
I have a class derived from slider which uses a custom control template and
I would like to create a custom WPF control that inherits from comboBox. So
I have a custom control that needs a minor adjustment when running on XP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.