Bulli
Bulli

Reputation: 125

How to disable the possibility to set Properties for Controls?

In my application I want to prevent other programmers to set properties to my Usercontrols that maybe cause some troubles.

A simple example is that I don't want to allow that someone set UseLayoutRounding to true.

<Button UseLayoutRounding="True"/>

I want to disable that Intelisense shows me UseLayoutRounding.

Upvotes: 1

Views: 234

Answers (1)

dvvrd
dvvrd

Reputation: 1689

You may override OnPropertyChanged method and throw exceptions when someone tries to change it:

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
    if (e.Property == UseLayoutRoundingProperty && (bool)e.NewValue)
    {
        throw new PropertyIsImmutableException("UseLayoutRounding");
    }
    //....
    base.OnPropertyChanged(e);
}

Or you may use new keyword:

public new bool UseLayoutRounding
{
    get { return (bool)GetValue(UseLayoutRoundingProperty); }
}

However, using second aproach without first remains posibility to change value like this:

yourSuperControl.SetValue(SuperControlType.UseLayoutRoundingProperty, true);

Upvotes: 1

Related Questions