views:

49

answers:

1

I have a custom control called TextBoxWithLabelAndUnits. From the name, you can tell that it's a TextBox that has a Label before it and a Label after it, so that the custom control looks like this:

       -----------------
Label: |               | units
       -----------------

I expose several dependency properties to configure the control, such as:

LabelWidth
LabelText
UnitText
TextBoxWidth

Now I have a user control called LatLong that I'm using for Latitude/Longitude input. The XAML looks like this:

<UserControl ...>
    <StackPanel>
        <TextBoxWithLabelAndUnits LabelText="Latitude:"
                                  UnitText="degrees"
        />
        <TextBoxWithLabelAndUnits LabelText="Longitude:"
                                  UnitText="degrees" 
        />
    </StackPanel>
</UserControl>

It will create a user control that looks like this:

           -----------------
Latitude:  |               | degrees
           -----------------
           -----------------
Longitude: |               | degrees
           -----------------

Now I want to use my user control in a project. However, I'd like the user control to expose properties so I can change the labels if I don't like the default settings. I can expose each one as a new dependency property to look like this:

LatitudeLabelWidth
LatitudeLabelText
LatitudeUnitText
LatitudeTextBoxWidth

LongitudeLabelWidth
LongitudeLabelText
LongitudeUnitText
LongitudeTextBoxWidth

And the XAML for something that uses a LatLong control will look like this:

<Window ...>
    <LatLong LatitudeLabelText="Latitude (in degrees)"
             LatitudeUnitText=""
             LongitudeLabelText="Longitude (in degrees)"
             LongitudeUnitText=""
    />
</Window>

But that seems like a lot of work since I have to re-expose every dependency property for each instance. I was wondering if there was an easier way that exposes the TextBoxWithLabelAndUnits instance itself, so I can edit the properties directly on it and use XAML that looks like this:

<Window ...>
    <LatLong Latitude.LabelText="Latitude (in degrees)"
             Latitude.UnitText=""
             Longitude.LabelText="Longitude (in degrees)"
             Longitude.UnitText=""
    />
</Window>

So in other words, rather than exposing the properties on each custom control inside the user control, I expose the custom control itself and get access to all the properties using dot notation.

Does anybody know if that's possible to do in WPF?

+3  A: 

Sure -- why don't you just give your LatLong these two properties?

public TextBoxWithLabelAndUnits LatitudeControl
{
    get { return latitude; }
}

public TextBoxWithLabelAndUnits LongitudeControl
{
    get { return longitude; }
}

Then they will be directly accessible through code.

Dan Tao
Hi Dan, I tried that and although it allows you to access the control in code, I need the ability to set properties in XAML as well. Any idea what I can do in that case?
Daniel T.