Exponer propiedades internas de objetos en WPF

I have the following UserControl

        <Label Content="Label"
               Name="myLabel" />

1) Expose the FontFamily propiedad de myLabel as it was the property of MyUserControl.
2) * (really optional) Define that the myLabel Content be "MyUserControl1", "MyUserControl2" etc, like the designer does when multiple controls are added.

How should I expose then that property, like a ordinal property(?) or dependency one(?)...

preguntado el 09 de marzo de 12 a las 15:03

2 Respuestas

Expose them as Dependency properties, actually every property on controls should be exposed as dependency property. Then you can use binding for example to attach the property to Label.FontFamily propiedad:

public void MyUserControl()
    myLabel.SetBinding(Label.FontFamilyProperty, new Binding {
                                                          Path = "FontFamily",
                                                          Source = this,

aquí FontFamily in Path setter is the name of your property.

P.S.: Didn't really got your second question. Can you either rephrase it or provide some sample?

respondido 09 mar '12, 16:03

second question. Say you put a Label in the Designer. the name, and the text is "Label1"... Now, you put another, the name and text will be "Label2" etc. I need that the text of my Label behave in the same logic. - sergio

FontFamily is declared on Control, which is the base of both UserControl and Label, and is an inherited DependencyProperty. So unless you override the FontFamily value on the Label (local value, Style, Trigger, etc.) you can already set FontFamily on the UserControl and have it show up on the Label without doing anything else. If you need to modify it internally and have it push out to show up on the UserControl you'll need to bind the UserControl's FontFamily to the Label's with Mode=TwoWay.

To the second part - you'd need to set up some counting mechanism that increments whenever an instance is created, possibly as a static field inside the control. You could then increment the value and apply it to the Label in the UserControl constructor (this would be dependent on loading order and variable at runtime during each session).

respondido 09 mar '12, 21:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.