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 7713031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:46:10+00:00 2026-06-01T01:46:10+00:00

I am trying to setup a custom attribute in C# to set whether a

  • 0

I am trying to setup a custom attribute in C# to set whether a business object property is editable, eventually enabling or disabling ReadOnly a textbox in XAML. Since (I thought) IsEditable was already implemented in System.Windows.Controls, i thought this would work:

[AttributeUsage(AttributeTargets.Property)]
public class EditableAttribute : Attribute
{
    public EditableAttribute(bool isEditable)
    {
        this.ReadOnly = !isEditable;

    }
    public virtual bool ReadOnly { get; set; }
}

Well, go figure, it doesn’t. I set [Editable(false)] to a string in an object and it is still editable. I have a feeling I’m not even close. Any help or suggestions would be greatly appreciated!

I am aware this can be setup as a style in xaml, but for this case it needs to be in the business object.

Thanks

  • 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-01T01:46:11+00:00Added an answer on June 1, 2026 at 1:46 am

    You can use BindingDecoratorBase to use custom binding and use an attribute.

    The following code is just me modifying my code in my project that uses custom validation . It probably should be refractored.

    public interface IEditatble
    {
        void SetValue(Control sender, DependencyProperty property);
    }
    
    [AttributeUsage(AttributeTargets.Property)]
    public class EditableAttribute : Attribute, IEditatble
    {
        public EditableAttribute(bool isEditable)
        {
            this.ReadOnly = !isEditable;
    
        }
        public virtual bool ReadOnly { get; set; }
    
        public void SetValue(System.Windows.Controls.Control sender, System.Windows.DependencyProperty property)
        {
            sender.SetValue(property, this.ReadOnly);
        }
    }
    

    You can create a custom binding:

     public class ReadonlyBinding : BindingDecoratorBase
     {
        private DependencyProperty _targetProperty = null;
        public ReadonlyBinding()
        : base()
        {
            Binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        }
    
        public override object ProvideValue(IServiceProvider provider)
        {
            // Get the binding expression
            object bindingExpression = base.ProvideValue(provider);
    
            // Bound items
            DependencyObject targetObject;
           
            // Try to get the bound items
            if (TryGetTargetItems(provider, out targetObject, out _targetProperty))
            {
                if (targetObject is FrameworkElement)
                {
                    // Get the element and implement datacontext changes
                    FrameworkElement element = targetObject as FrameworkElement;
                    element.DataContextChanged += new DependencyPropertyChangedEventHandler(element_DataContextChanged);
                }
            }
    
            // Go on with the flow
            return bindingExpression;
        }
    
        void element_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            object datacontext = e.NewValue;
            if (datacontext != null && _targetProperty != null)
            {
                PropertyInfo property = datacontext.GetType().GetProperty(Binding.Path.Path);
                if (property != null)
                {
                    var attribute = property.GetCustomAttributes(true).Where(o => o is IEditatble).FirstOrDefault();
                    if (attribute != null)
                    {                        
                        Control cntrl = sender as Control;
                        ((IEditatble)attribute).SetValue(cntrl, _targetProperty);
                    }
    
                }
            }
        }
    }
    

    And you can use it like:

    [Editable(true)]
    public string Name { get; set; }
    

    Xaml:

    <TextBox  IsReadOnly="{local:ReadonlyBinding Path=Name}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to set a custom backend model for a custom attribute. The
I'm trying to setup custom validation for a checkbox. I have 7 checkboxes each
I am using jboss-as-7.1.0.Final-SNAPSHOT and trying to set up custom login module that uses
Hi I am trying to create a Custom Object in Salesforce.com Developer Edition, because
I'm trying to setup a custom site-package directory (Python 2.6 on Windows Vista). For
I'm trying to setup a player using mediaelement.js. I'm implementing my own custom playlist
I am trying to embed a flash video into a custom setup of the
I am trying to set up a custom entity, following along alan storms article
I'm trying to setup a relationship hierarchy between objects. Every object has a parent
I'm trying to call a custom action within my Setup & Deployment project to

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.