Click here to Skip to main content
15,911,039 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

Basically I have a custom user control similar to a button. With a button you can set the Content to place a string within your button:
XML
<Button Content="Hello World"/>

I want to do something like that, have a user control with among things a label and let the content property of the user control change the label's text.
I can't get it to work so far, I've tried a couple of things. I'm not really even sure what to google anymore.

Anyone an idea where I should look?
Or maybe some example code?
Posted
Comments
joshrduncan2012 13-Mar-13 16:42pm    
Can you show us what you have tried that doesn't work? We can't assume anything unless we see your effort towards solving this issue.
Sergey Alexandrovich Kryukov 13-Mar-13 17:15pm    
Good point. Not clear what's OP's problem. (We call it "trying to break through the open door" :-)
I somehow answered, please see.
—SA

1 solution

What you need to know here is the WPF content model:
http://msdn.microsoft.com/en-us/library/bb613548.aspx[^].

If you look at it, you will see that, for example, System.Windows.Controls.Button is derived from the class System.Windows.Controls.ContentControl, and, hence, it's content is "a single arbitrary object":
http://msdn.microsoft.com/en-us/library/system.windows.controls.button.aspx[^].

Your class derived from System.Windows.Controls.UserControl and, hence, also from the class System.Windows.Controls.ContentControl. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.aspx#inheritanceContinued[^],
http://msdn.microsoft.com/en-us/library/system.windows.controls.usercontrol.aspx[^].

It means that, basically, it is considered in the content model pretty much as the button; it's content is already "a single arbitrary object". What more "custom" would you need? And your custom control class also inherits the Content property. You don't need to make it more "custom" because it is already "custom enough":
http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content.aspx[^].

However, in practice, to make you Content working with the WPF UI properly, you should not make the content totally arbitrary. In most cases, developers of the user controls use some class of Panel as the content, more exactly, one of the classes derived from Panel; and the panel holds some other UI Elements:
http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx#inheritanceContinued[^],
http://msdn.microsoft.com/en-us/library/system.windows.uielement.aspx[^].

Any problems?

—SA
 
Share this answer
 
Comments
wpte 13-Mar-13 18:01pm    
Well with custom I pretty much mean something like an alternate implementation, an override for example.
With these links I could probably cook something up, definitely a good read.
Thanks
Sergey Alexandrovich Kryukov 13-Mar-13 18:18pm    
Oh yes, you can. Not a rocker surgery, this part. :-)

(The override is impossible just because Content property is not virtual. It does not need to be, due to two reasons: 1) the property is of the type System.Object, which is the base type for all other type); in other words, you can use any type at all; 2) the property is read-write, so you actually can assign any thinkable object to it. Conclusion: it is already fully custom.)

Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900