0

I have an issue with creating custom controls in wpf. I always intend to create custom controls completely in code-behind in case I need to extend them.

The issue is when I add them in the xaml they are editable. I don't know how to explain but when I click on them, I can write into them. I think it's the way I'm writing my custom controls. Here's a sample code.

public class MyCustomControl : UserControl
{
    public MyCustomControl()
    {
        var button = new Button();

        this.Content = button;    // I always assign the root element to the Content property of the UserControl. I think this is the case
    }
}

As I said when I add MyCustomControl in Xaml part, it works totally fine but when I click on it (again in xaml part) it is editable and I can write into it. What's my problem exactly?

[Edit] I added screen shots

MyCustomControl in normal state MyCustomControl when I click on and write down into it

this is the code-behind

public class MyCustomControl : UserControl
{
    public MyCustomControl()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.Content = new Grid() { Background = Brushes.Red };
    }
}
2
  • Can you provide a screenshot of your problem? Commented May 9, 2014 at 8:39
  • Sure. I added screenshots @samar. Commented May 9, 2014 at 8:53

1 Answer 1

1

It seems to me you can just overwrite the Content property as it is not locked in any way.

You could add a delegate on the Coerce of the Content and check using a bool check whether it is your code which is editing the control or some external source.

How to do this?

First read these articles:

Then, use something like this:

Control.ContentProperty.OverrideMetadata(typeof(YourControlName), new PropertyMetadata(null, null, yourCallback));

As another option, you could create the Control as Template (define the Control.Template inside the Control). In this way you don't have to depend on the Content property (It won't affect the template). You can even put in the passed in Content if you want, but you don't need to.

Sign up to request clarification or add additional context in comments.

9 Comments

That's a nice idea. I understand what you mean however I do not know how to implement the part "Adding a delegate checking whether it is your code or some external source." could you provide me a sample please?
@user3530012: Found some sources how to do this. See the updated answer.
Well the problem is that Content property is accessible from xaml. I've found a solution to hide a property from xaml part which is to use EditorBrowsable attribute. but it doesn't work on inherited properties. I tried to override the property setter (by using the new keyword) but id didn't help either.
@user3530012: XAML relies on the DependencyProperties, so you are stuck there. I will put another option in.
@user3530012: Did my last update help you? Where are you stuck?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.